I'm trying to filter out invalid user inputs in a small C++ program using the following chunk of code:
int selection = -1;
while (!(selection >= 1 && selection <=4))
{
cin >> selection;
if (!(selection >= 1 && selection <=4))
{
cout << "invalid selection!" << endl;
cout << "selection: ";
}
}
It seems to work fine when I enter any numerical value that is either inside or outside the range i want to filter. However strange things happen when I enter invalid values such as values larger than the maximum storable int or characters. The code loops through and skipping the "cin" command.
How do I fix this?
Thanks
You need to detect unconvertible input using fail() and then ignore the rest of the bad data and reset cin error flags using clear() before reading in a new input attempt.
int selection = -1;
while (!(selection >= 1 && selection <=4))
{
cin >> selection;
if (cin.fail() || !(selection >= 1 && selection <=4))
{
cout << "invalid selection!" << endl;
cout << "selection: ";
cin.clear();
cin.ignore(std::numeric_limits<int>::max(), '\n');
}
}
Here are two suggestions for fixing your issue:
cinThe common solution is Read As String And Parse, but I'm presenting both for you to choose.
cinWhen the stream extraction function receives a character that is not suited for numerics, it sets a fail bit. You need to check the state of the stream (cin) for failure. If you want to continue, you need to clear the error state.
The state can be checked by using the fail method: cin.fail(). To clear the state use: cin.clear().
See C++ Online Reference -- istream
An alternative is read the input as a string, then parse the string for your data. The string container has some useful methods for parsing.
Use getline to read in a string variable from cin.
Again, you will have to write code to check for errors and process them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With