|
-
February 13th, 2008, 09:48 PM
#1
standard input question
Hello guys
I have a question about.
if a program asks the user to input a number,
but the user enters a character,
the istream is in error state, am I correct?
my question is, how do I clear it?
Code:
#include <iostream>
using namespace std;
int main()
{
short num;
cout << "Enter a number: " << endl;
cin >> num;
cin.clear();
cout << "Enter a number again: " << endl; // line A.
cin >> num;
return 0;
}
for example,
if I input numbers greater than the size of short,
line A works and waits for an input,
but if I enter any characters,
line A displays but it does not wait for the input and just exits the program.
why is that?
thanks for the help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|