Jump to content
  • Advertisement
Sign in to follow this  
robert_p

dealing with bad input using iostream

This topic is 4959 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

suppose i want to get an int and the user enters a char. my program bugs up hard when it happens. i am doing if ( cin.fail() || cin.bad() ) cin.clear(); redo the input... but if i wanted an int and the user enters a char this doesnt do anything and my cin's fail from then on. i seen this as the solution but it doesnt seem to work. any help plz??

Share this post


Link to post
Share on other sites
Advertisement
The way you should deal with input is to treat it all as a string. Then once you have it you can convert it to an int if you need to. This way your program will work fine no matter what the user enters.

Share this post


Link to post
Share on other sites
I'm curious. I only recently came up with that method after looking at the SGI STL documentation for a replacement for fflush in C++. However, most sources I've seen attempt to do this using cin.ignore(). I went with cin.sync() because it doesn't require a loop to make sure that all data is cleared and is therefore more succinct. Is there anything important that I missed in this evaluation (such as sync() taking an eon to finish or not working as I intended in certain cases)?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!