cin skips over the next cin only when a non-numeric value is entered

clbx

The following code works as intended when it is given 2 integers, however if a non numeric value (like 'a') is given, it skips the second cin.

int num1;   // lesser integer value input by user
int num2;  // greater integer value input by user

cout << "\n\nNumber 1: ";
cin >> num1;


cout << "Number 2: ";
cin >> num2;

if (!cin)
{
    cout <<"\nError" <<endl;
    return 0;
}

When entering a number for the first prompt the program carries on, however if something like a is entered for the first prompt, it skips the second prompt and hits the error condition

Some programmer dude

When the formatted input operator >> fails (like for example you give a as input when a number was expected) the input in the buffer is not removed, it's still there for the next time you want to read input (which will attempt to read the very same a again).

The flags are also not cleared automatically.

You could solve this by checking when you read the input:

if (!(std::cin >> num1))
{
    // Failure of some kind
    if (std::cin.eof())
    {
        // End of file, handle this any way you like or need
    }
    else
    {
        // Not end-of-file
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Skip bad input
        std::cin.clear();  // Clear error flags
    }
}

References:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

std::cin skips white spaces

Default value for variable in C++ using cin >>

Providing default value of cin input

cin overwriting my initialized value when it reads wrong type?

Why is cin.ignore() necessary when using "getline" after "cin", but is not required when using "cin" multiple times?

Issue When Clearing cin Buffer After Using cin.getline()

Next() skips the actual and next item in list, when it should only skip the actual item

Cin input with a + causes next input to be a string

Segfault when puting cin value into array

Pick next line from cin

Why is it "bad" to use cin for numeric variables?

cin infinite loop when reading in a non-numeric value

getline and cin used one after another skips next inputs

skipping over my last cin input

First & only cin is skipped, even after using cin.ignore

Get only the first value from std::cin

C++ - cin to only take one integer

while( cin >> value ) breaks the loop when i entered a character

What will happen when we declare cin as int and take input from cin with print cin in cout?

cin >> ws vs cin.ignore(numeric_limits<streamsize>::max(), '\n')?

Cin overflowing into following cin

While loop skips cin.getline() for C-string

cin skips when a character is input

cin running only once inside while loop

how to make cin only take integer inputs

cin is being ignored when another cin was previously used

Typescript .sort() skips over a value

C++ how to make it so a double variable can only have numbers entered in using cin

How to iterate over an vector and then compare it with a cin?