Input (istream) error testing
-
I want to test for an invalid character in entered numeric (integer) data, (e.g., enter "d23", instead of "123") In the following code, the do loop loops infinitely after an invalid entry, "d23". Seems like cin is not stopping to accept more input the second time around. Can anyone tell me what I'm doing wrong? Thanks, Tom ------------------------ #include using namespace std; void main() { int x; cout << "Type in an integer value: "; cin >> x; while (cin.fail()) { cin.clear(); cout << "Invalid character.\n Type in an integer value: "; cin >> x; } cout << x << endl; }
-
I want to test for an invalid character in entered numeric (integer) data, (e.g., enter "d23", instead of "123") In the following code, the do loop loops infinitely after an invalid entry, "d23". Seems like cin is not stopping to accept more input the second time around. Can anyone tell me what I'm doing wrong? Thanks, Tom ------------------------ #include using namespace std; void main() { int x; cout << "Type in an integer value: "; cin >> x; while (cin.fail()) { cin.clear(); cout << "Invalid character.\n Type in an integer value: "; cin >> x; } cout << x << endl; }
hain wrote:
cin >> x; while (cin.fail())
What about:
while (! (cin >> x))
{
cin.clear();
while (cin.get() != '\n');
}"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
hain wrote:
cin >> x; while (cin.fail())
What about:
while (! (cin >> x))
{
cin.clear();
while (cin.get() != '\n');
}"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
hain wrote:
cin >> x; while (cin.fail())
What about:
while (! (cin >> x))
{
cin.clear();
while (cin.get() != '\n');
}"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Thanks David, terrific, that works perfectly! Problem is, your solution is mysterious to me... I don't understand it. Why doesn't the get() move the stream pointer forward (after an error), so that by the time we get back to cin>>x, the extraction starts after the '\n' (i.e., nothing is extracted)? Thanks again, Tom
-
Thanks David, terrific, that works perfectly! Problem is, your solution is mysterious to me... I don't understand it. Why doesn't the get() move the stream pointer forward (after an error), so that by the time we get back to cin>>x, the extraction starts after the '\n' (i.e., nothing is extracted)? Thanks again, Tom
cin >> x
stops at the first whitespace, socin.get()
is used to get passed all that."Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
cin >> x
stops at the first whitespace, socin.get()
is used to get passed all that."Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne