Start New Discussion within our Software Development Community

I wrote a program that allow users input a double value, if I input a string or a char value, it turns out to be a logical error. I've tried this:

bool h;
float value;
do{
cout<<"enter a value"<<endl;
cin>>value;
if(cin.fail()){
cout<<"error, enter a value!"<<endl;
cin.clear();
cin.ignore();
h=true;}
else
h=false;
}
while(h==true);

If I input "mee" for instance it gives me "error, enter a value!" thrice instead of once. I thought cin.ignore(), cin.clear() is used to erase the input stream. Thanks in advance!

Try
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

thanks a lot, it worked.

This article has been dead for over six months. Start a new discussion instead.