iostream cin.clear does not clear input stream

Hi folks,

I've been searching the Internet for the answer to a common problem. When I use cin in a program, cin receives an invalid character without the user providing any input. Although using cin.ignore may solve the problem, it is a poor design solution. Ideally, the user should be able to walk away from the computer, come back in an hour and be able to type in the value. That's how most C++ applications work. Does anyone know a better solution than cin.ignore?

#include <iostream>
using namespace std; int main (void)
{
int first_value;
cout << "Enter an integer: ";
cin.clear();
cin >> first_value;
cout << "integer is" << first_value << endl;
}

Thanks in advance.
You got how c++ works, but I think you running into a different issue.

A normal console application on most of the modern operating systems wouldn't act that way unless something is interfering with it, like a debugger or other background software. Either software would be trying to wrestle standard IO from the app because it took too long for a security measure or debugging cycle.
Thank you for your response. I am using the on-line compiler at www.codepad.com. It compiles and executes C++ code and displays the results. I think you are correct.
Topic archived. No new replies allowed.