Archived

This topic is now archived and is closed to further replies.

Ensuring safe input in a C++ Console window

This topic is 5618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, How can I ensure when I ask for cin << that I get the value I'm looking for? For example, int i; cin >> i; if The person enters an F, the program isn't goign to be able to evaluate properly. So how can I make sure I get safe input? [edited by - Estauns on July 26, 2002 1:48:37 AM] [edited by - Estauns on July 26, 2002 1:49:28 AM]

Share this post


Link to post
Share on other sites
You need to read in the whole line as a string and then
carefully examine each character to reject those "unsafe"
entries.

You very rarely see "cin >> i;" in any code that does
error-checking. OK, make that NEVER.


~~~~
Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
look at the documentation for istream::fail(). it''s what you want.



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
quote:
Original post by tangentz
You need to read in the whole line as a string and then
carefully examine each character to reject those "unsafe"
entries.


Ew. No. Don''t worry, there are functions for that.
quote:

You very rarely see "cin >> i;" in any code that does
error-checking. OK, make that NEVER.

Untrue. iostreams may be limited in the syntax they can handle, but they are quite usable in robustly error-checked programs. If they weren''t, they wouldn''t be in the standard.



Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
Untrue. iostreams may be limited in the syntax they can handle, but they are quite usable in robustly error-checked programs. If they weren''t, they wouldn''t be in the standard.



Well, if you think they are good enough, that''s OK.
I wouldn''t waste my time to try to convince you otherwise.

BTW, The C++ Standard is full of "defects" and "oversights"...

~~~~
Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
You can still run into the endless loop problem when using cin as well, I see people do this all the time. Example:


    
int main(void)
{
int i;

while(1)
{
if(cin >> i)
{
cout << "You entered: " << i << endl;
}
else
{
cout << "Bad Input!" << endl;
}
}

return 0;
}


Run the above, and enter some bad input. Whoops, infinite loop.

You see loops like this when people have a menu for their program.

There's two ways to properly handle console input into non-text variables:

1) Check the result, and if the input fails, read a line of text from the input and discard it, so you don't read the same thing next time. This is a pain, because you have to put a few lines of code to read and discard the bad data at every place you do a cin.

2) Read a line of input to begin with, then attempt to parse it for the values you want (I love sscanf). If it fails, then you can print the failure message and go back into the read loop, since the bad data will have been removed from the input buffer.


[edited by - cgoat on July 26, 2002 2:10:54 PM]

Share this post


Link to post
Share on other sites
Exceptions? We don''t need no stinkin'' exceptions!

Even if you caught an exeption, if you plan to continue to try and read again, you have to clear that bad input off the buffer.


http://www.goatsoft.com
Try my Tri-Towers clone

Share this post


Link to post
Share on other sites
quote:
Original post by cgoat
You can still run into the endless loop problem when using cin as well, I see people do this all the time. Example:



      
int main(void)
{
int i;

while(1)
{
if(cin >> i)
{
cout << "You entered: " << i << endl;
}
else
{
cout << "Bad Input!" << endl;
}
}

return 0;
}


Run the above, and enter some bad input. Whoops, infinite loop.



That would produce an infinite loop regardless of the cin.

Share this post


Link to post
Share on other sites
quote:
Original post by wild_pointer
If you put in bad input, it wont ask for input again. The program will just spit out "Bad Input" forever.




The same would happen with C stdio functions. But the above code IS an infinite loop, there is no (graceful) way out of it.

[edited by - sark on July 27, 2002 1:00:19 AM]

Share this post


Link to post
Share on other sites
Right, that was my point. Regardless of whether you''re using iostream or stdio the best way to "ensure safe input" is to read a line of text into the buffer, then attempt to parse it into the values you want. This way you can avoid such an infinite loop on bad input.

Share this post


Link to post
Share on other sites