Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering out invalid user inputs

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

like image 554
Faken Avatar asked Jun 08 '26 22:06

Faken


2 Answers

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');
    }
}
like image 74
Steve Townsend Avatar answered Jun 10 '26 10:06

Steve Townsend


Here are two suggestions for fixing your issue:

  1. Add Error Handling to cin
  2. Read as String And Parse

The common solution is Read As String And Parse, but I'm presenting both for you to choose.

Add Error Handling to cin

When 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

Read As String And Parse

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.

like image 38
Thomas Matthews Avatar answered Jun 10 '26 10:06

Thomas Matthews



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!