crisponions
Technical User
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');
}
}
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');
}
}