I am trying to validate input, to accept only integers, and its working fine for letters and decimal points after 4. For example if I enter 1.22 it will read number one only and go to infinitive loop, but when I enter number what are bigger than 4, for example 5.55 it is working good then, how can I solve this problem? Thank you and appreciate your help!
void Furniture::getSelection()
{
do {
cout << "\nWhich object would you like to measure:\n"
<< "1.Table\n"
<< "2.Stool\n"
<< "3.Bookshelf\n"
<< "4.Exit\n" << endl;
while(!(cin >> choice)) {
cerr << "The format is incorrect!" << endl;
cin.clear();
cin.ignore(132, '\n');
}
while(choice != 1 && choice != 2 && choice != 3 && choice != 4) {
cerr << "Invalid Input!!Try again\n" << endl;
break;
}
} while(choice != 1 && choice != 2 && choice != 3 && choice != 4);
choiceis anintthen entering "1.22" will leave the ".22" in thecinbuffer aftercin >> choice. You need to get rid of that whether or not the insertion succeeds or fails. Or, see comment #1.while(choice != 1...should be anif. And the condition can be shortened toif (choice < 1 || choice > 4)....