Basically I'm trying to make error messages for invalid cin inputs, but I'm finding that when I put letters after numbers for integer values it just reads the numbers and then carries the letters over to the next line. I want that input to make cin fail instead, but I have no idea how to do that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main()
{
int x = 0;
cin >> x;
while (!(cin))
{
cin.clear();
cout << "Invalid";
cin.ignore(1000, '\n');
cin >> x;
}
}
This code puts out invalid for letters, for letters then numbers, but not for numbers then letters. ie "100fdsjksfd" is a valid input, and I want that to make cin fail as well.