In the following loop, if we type characters as the cin
input instead of numbers which are expected, then it goes into infinite loop. Could anyone please explai
Another alternative is operator! ,it is equivalent to member function fail()
//from Doug's answer
if ( !cin )
{
cout << "ERROR -- You did not enter an integer";
// get rid of failure state
cin.clear();
// From Eric's answer (thanks Eric)
// discard 'bad' character(s)
cin.ignore(std::numeric_limits::max(), '\n');
}