First, your issue is that
Persons ac;
on line 103 does not have any valid information in it other than what's there by default. You need to put actual information in there before attempting to save it.
But, more importantly, your
Persons class (which, by the way, is just a single Person) is NOT a "POD" type. This essentially means it contains pointers, as you have with std::string. Aside from short-string optimization, the actual string data is not stored directly in the std::string's bytes, so you shouldn't directly write a std::string to file as you do on line 106.
Instead, you know how you already have your print_info function?
What you can do is instead of printing it to cout (stdout), you can generalize it to print to any output stream.
1 2 3 4 5 6 7 8 9
|
void print_info(ostream& out)
{
out << "\n ID : " << Id
<< "\n Name : " << Name
<< "\t Age: " << Age
<< "\t DOB: " << DOB
<< "\t Position: " << Position
<< "\t Gender: " << Gender;
}
|
Then you can:
1 2 3 4 5 6
|
Person person;
// Fill in information for person
ofstream myfile("File1.txt", ios::app);
person.print_info(myfile);
|
However, doing it this way will be more complicated if you need to read from the file in the future. Is reading from the file part of the requirements of the assignment?
Enter Name (20 characters): "; |
Note that nothing in the program prevents the user from typing in more than 20 characters. I'm wondering if your instructor is assuming that small-string optimization will be used here.