Jump to content
  • Advertisement
Sign in to follow this  
villiageidiot

Sanity Checking User Input

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

I was wondering what the best way to sanity-check user input is (using C++). For example, say a program requests the user to enter a number (1, 2, or 3) as a choice for a menu. What is the best way to check to make sure that the user has not entered something other than a number (like a string, or just random characters)? The trouble I have is that whenever I try to validate user input, I can always enter in some random collection of letters / numbers that causes the program to skip ahead or just crash altogether, which is a bad thing. Any advice?

Share this post


Link to post
Share on other sites
Advertisement
Step-by-step!

1)read everything in as a string

2)Check to see if each element of the sring is in the form you want.

3)If string is not in the form you want go to step 1

4)Use stringstreams to enter the string as data into the variable

5)Profit

Share this post


Link to post
Share on other sites
Allright, thanks - I think I've got it now. Sorry for the stupid question...I ended up finding the answer a few minutes after I posted this. My bad!

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!