no user input throw an exception

i have a program i am working on
int main ()
{
int input=0;
cout <<"pick a number ";
cin>>input ;

}
how do i check to make sure there is a value inputed in from the user and not just the return key pressed. So if only the return key is pressed throw a error or exception, i cannot get it to work.
cin's extraction operator will keep waiting for input even if the user simply presses enter. If they enter invalid input, though, it will set cin into a state of error, which you can check and clear.
so how do what would you suggest?

string in
getline(cin,in);
input=in;
if (in=='10') or '\n'
{some code }
Last edited on
iostream's handling of newlines is FUBAR.
Get a string somehow, and use sscanf once you have your string.
1
2
3
4
5
6
7
8
string in;
int input_value;
getline(cin,in);
if(sscanf(in.c_str,"%d",input_value)==1){
	//do stuff with valid integer input_value
}else{
	//tell the user they didn't put in a good integer.
}

Last edited on
That's really a great information you have posted here. keep it up.
Topic archived. No new replies allowed.