I'm using a for
loop to try to create a "blank" file of 10 records where the values are set to 0 or blank using a constructor from my class, found below:
class employee {
char name[30], hire_date[30], salary[30];
int ID;
public:
employee(string = "", int = 0, string = "", string = "") {
void setHireDate(string);
string getHireDate();
void setname(string);
string getName();
void setsal(string);
string getsal();
void setId(int);
int getId();
}
See below the beginning of int main()
. The section where my program supposedly creates the blank database file
int main()
{
int a, id;
string name, hire_date, salary;
cout << "Welcome to Mahendra Pruitt's Employee Database!\nEnter a number for the option you would like to select:\nPlease choose option 0 if you haven't already chosen that one.\n" <<
"0 - Create a blank new database file that will overwrite all existing data." << endl
<< "1 - Choose employee IDs (1-10) and change or add information to their file" << endl
<< "2 - Select whether or not you would like to update, view, or add a new record." << endl;
cin >> a;
// creating a blank database file
employee blankelist;
if (a == 0) {
while (a == 0) {
if (a == 0) {
ofstream outdatabase("database.dat", ios::out | ios::binary);
for (int i = 0; i < 10; i++) {
outdatabase.write(reinterpret_cast <const char *> (&blankelist), sizeof(employee));
}
cout << "Enter the number 1 or 2: " << endl
<< "1 - Choose employee IDs (1-10) and change or add information to their file" << endl
<< "2 - Select whether or not you would like to update, view, or add a new record." << endl;
cin >> a;
}
}
}
Throughout the rest of my program, I use a separate instance of employee that i call "employed" ... see example of one of my functions, whose purpose is to output stored information to be viewed:
void outputLine(ostream &output, employee &employed, int i, fstream &readfromfile)
{
int id = i;
readfromfile.seekg((id - 1) * sizeof(employee));
readfromfile.read(reinterpret_cast <char *>(&employed), sizeof(employee));
output << "ID: " << employed.getId() << endl
<< "Name: " << employed.getName() << endl
<< "Salary: " << employed.getsal() << endl
<< "Hire date: " << employed.getHireDate() << endl;
}
When I run the option to create these blank files and try to view a record, it rather shows numbers/gibberish... See output below:
Enter your choice:
1 - Update an account
2 - Add a new account
3 - View record
4 - End program
3
Which record would you like to view? (1-10). Enter 0 to go back to the menu.:
1
ID: -858993460
Name: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
Salary: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
Hire date:
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
How do I get my program to set it up so that whenever I run the section that creates blank records, it creates them, and if I try to view the record, it shows up as blank?
I've tried the following statement as well:
outdatabase.write(reinterpret_cast <const char *> (&employed), sizeof(employee));
instead of
outdatabase.write(reinterpret_cast <const char *> (&blankelist), sizeof(employee));
In my for
loop, but that didn't work either.
Looking forward to your responses!
In constructor employee::employee()
you are declaring some functions instead of calling them. It appears you need to call some member functions and those functions should set the members to a sensible value.
BTW you should learn serialization and deserialization of classes for such tasks.
Further readings: