How to flush cin in c++ properly?

38 Views Asked by At
int n;
cin>>n;
if(cin.fail()){
 cin.clear(); 
 cin.ignore(numeric_limits<streamsize>::max(),\n);
 cout<<"Data is not numeric";
}
else{
 cout<<n;
}



This works fine until any numeric value is entered followed by some characters like: 123ahs. here else block will also execute and cout will give me 123. So what shall i do to discard this kind of input as well?

0

There are 0 best solutions below