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!

Recommended Answers

All 4 Replies

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

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

thanks a lot, it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.