int main(int argc, char *argv[])
{
char ans='y';
Student sobj;
//We open student.dat in append mode
/*ofstream out("student.dat", ios::app | ios::binary);*/
cout << "Enter the Filename you want to write to: " << endl;
cin >> fname;
ofstream out(fname, ios::app | ios::binary);
if(out.is_open())
{
//Loop will continue until something other then y is entered
while( ans == 'y')
{
cout << endl << "Continue ?";
cin >> ans;
if(ans == 'y')
{
sobj.GetStudentData();
out.write((char*) & sobj, sizeof(sobj));
}
}
}
out.close();
After the user inputs the value of ans, they press enter. That leaves a newline character sitting waiting in the input buffer. The next getline reads up to the newline (which is then discarded) meaning an empty line is read.
The purpose of using cin.ignore() is to get rid of that unwanted newline character.