I have a person class which writes a name and age to file using binary I/O. By default I need it to create 10 slots for 10 people each set to "null" for name and 0 for age. Then it asks if you want to modify a record and if so which one. First, I need help setting my char array defined in my class to "null" within the default constructor. Second I need to know why my "for" loop for creating the 10 slots isn't working. Code is below
class person //class of persons
{
protected:
char name[80]; //person's name
short age; //person's age
public:
void getData() //get person's data
{
cout << "Enter name: "; cin >> name;
cout << "Enter age: "; cin >> age;
};
person();
};
int main()
{
char again;
int num;
person pers; //create a person
int position;
ofstream outfile("persons.txt", ios::binary);
outfile.seekp(0);
for(int i = 0; i < 9; i++)
{
outfile.write((char*)(&pers), sizeof(pers));
}
cout << "Would you like to modify a record? (y or n): ";
cin >> again;
while (again == 'y')
{
cout << "Which record would you like to modify? (1-10): ";
cin >> num;
position = (num-1) * sizeof(pers);
outfile.seekp(position);
pers.getData();
outfile.write((char*)(&pers), sizeof(pers));
cout << "Would you like to modify another record? (y or n): ";
cin >> again;
}
_getch();
return 0;
}