Sign in to follow this  
villiageidiot

Sanity Checking User Input

Recommended Posts

villiageidiot    132
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
nobodynews    3126
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

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