I need some help with cin.fail()
I'm making a program that accepts an integer and asks for in to be put in again if it is not an integer. My code:
Expand|Select|Wrap|Line Numbers
- do
- {
- valid = true;
- cout << "Enter an integer: ";
- cin >> number;
- if(cin.fail())
- {
- cout << "Error! Please enter integer value! " << endl;
- cin.clear();
- cin.ignore(50, '\n');
- valid = false;
- }
- }
- while(!valid);
It only works if the first 'charater' (?) is a character,
i.e. number = i123
but if number = 123i321
then is takes number ase 123, which is not what i want. I want it to ask for a new input.
I hope its possible, i'm assuming it'll be a simple fix, thanks for any help given.