Questions about validating inputs

When I try to cin three integers and cout “not valid” after entering all three of the inputs, it turns out that as soon as the first integer is not valid(for example if I enter a double type), the “not valid” will be immediately cout without allowing me to enter the other two inputs.

#include <iostream>
using namespace std;

int main(){

int num1, num2, num3;
Bool done = false;
while (!done)
{
cin >>num1>> num2>> num3;
If (cin.fail())
{
cout << “not valid” << endl;
done=true;
}
else
{
cout << “valid” << endl;
}

return 0;

}

Correct. Once stream extraction can't properly extract, it fails the stream. Before further stream extraction can be performed the stream state has to be set to good and the invalid data removed from the stream.

So if the stream extraction fails on say extracting num1 then you don't extract num2 or num3.
Last edited on
Topic archived. No new replies allowed.