I have to accept three numbers from the user using cin into 3 variables.
If the user enters non integers,I must reject it and ask for a new one instead of that.
So,here is my attempt:
int a,b,c;
do{
cin.clear();
while(cin.get()!='\n');//remove failure bits,clear stream if wrong input
cin>>a;
}
while(!cin);//while the entry isn't good
do{
cin.clear();
while(cin.get()!='\n');
cin>>b;
}
while(!cin);
do{
cin.clear();
while(cin.get()!='\n');
cin>>c;
}
while(!cin);
I expected it to work,but it takes 4 numbers instead of 3.
Why?
What is wrong?