C++ Verify user inputs a number

Started by
17 comments, last by Justaddwater 20 years, 1 month ago
with your code how would i modify it to do a check? I cant tell where to put in the line

if (num < 1) output error and redo

[edited by - justaddwater on March 14, 2004 1:29:09 AM]
Advertisement
In the conditional.
Ive never seen a statement written like that before with the cout in the while condison , so im not sure hoe you mean I could add it in there...
The "cout << foo" part returns a true value, so it doesn''t affect breaking out of the loop - it''s used for its side effect, i.e. printing the text. The other guy is putting it there so that text will output before the input is checked, even off the first iteration.

All you need to do is add all of your filters to that line, chained with logical OR''s.

so at a high level it looks like
while (do output and report true) and ((input was not an integer) or (next character after the input isn''t a newline) or (anything else that would render the input invalid <-- YOU FILL IN THIS PART))  report error; // because "do output and report true" was of course true, and one of the filters failed, making that part true as well  clear the failed state of the input stream; // so we don''t report an error next time  skip ahead in the input stream to the next newline; // so we don''t re-parse the invalid data and get stuck in a loop

I understand now, thanks for the info and the neat new trick for i/o (new to me!)

I added this to my program and it worked great, but there is one problem now with it.

When the num < 1 triggers you have to hit CR twice to get back to prompt, when any other condishon triggers it loops right back to prompt

while (cout << "Enter Quantity: " && !(cin >> Quantity)  || (Quantity < 1) || cin.peek() != '\n'){    	cout << "Quantity must be  whole positive number!" << endl;    	cin.clear();	cin.ignore();	cin.ignore(numeric_limits<streamsize>::max(), '\n');}


[edited by - justaddwater on March 15, 2004 10:32:21 PM]

[edited by - justaddwater on March 15, 2004 10:33:39 PM]

[edited by - justaddwater on March 16, 2004 6:01:15 PM]
Unpatched VC6?

Go here and here to fix it.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by Justaddwater
I added this to my program and it worked great, but there is one problem now with it.

When the num < 1 triggers you have to hit CR twice to get back to prompt, when any other condishon triggers it loops right back to prompt

*snip*



Err, what''s with the two ignore lines?

The program works perfectly for me if you comment out/delete the first line.
dude, why is it always the stupid little things like that that trash it!?!?!

Thanks much for spottin that extra ignore!

This topic is closed to new replies.

Advertisement