Sign in to follow this  

Sanity Checking User Input

This topic is 4610 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
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
*bUrp* (not my usual burp, which has to do with source file organization, but an equally valuable bookmark)

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

This topic is 4610 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this