-
getline help!
I have this function for my address book program but the first getline isn't working, somebody please tell me why!
PHP Code:
void ChangeEntry (sturec &book, fstream myfile, int &recordnum)
{
int whichnum;
char temp1[50], temp2[50];
cout << "\nRecord number to change? ";
cin >> whichnum;
if (whichnum > recordnum)
cout << "\nI'm sorry, there are not that many records. \nPlease select a record"<<
" between 0 and "<<recordnum<<endl<<endl;
else
{
myfile.seekg(whichnum*98);
myfile.read(temp1, 15);
cout <<"Last name: "<<temp1;
cout <<"\nLast name? \n";
cin.getline (temp2, 15);
cout << temp2;
if (strcmp(temp2, "")!=0)
strcpy (book.lastname, temp2);
myfile.read(temp1, 15);
cout <<"First name: "<<temp1<<endl;
cout <<"First name? ";
cin.getline (temp2, 15);
if (strcmp(temp2, "")!=0)
strcpy (book.firstname, temp2);
}
}
-
because there is still a newline char in the input buffer following the line:
cin >> whichnum;
to fix it add this after the above line:
cin.ignore();
As you get more sophisticated with your programming skills read about istream methods (>> and getline in particular) in your favorite textbook (as there are some picky ooni details that can trip you up if you don't know how these methods work), or do a search on this board using getline as the searchword.
-