Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie C++ cin question

Status
Not open for further replies.

crisponions

Technical User
Jul 2, 2003
9
US
I am finishing up the code for a intro c++ class and have just one last hurdle. I have a function to get a name and assign it to a char array (requirement is to use char array), problem is if I hit enter without any other input it causes an endless loop. I know it is because of the cin.ignore(100,'\n') line, but any advice as to how to fix this?

void getName(char name[])
{
// function to get name input
cout << "Enter the name of the food\n> ";
cin.get(name, MAX_SIZE);
cin.ignore(100, '\n');
while(!cin)
{
cin.clear();
cout << "\nInvalid input. Try again." << endl;
cout << "Enter the name of the food\n> ";
cin.get(name, MAX_SIZE);
cin.ignore(100, '\n');
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top