Validate Your User Input
When you create a program that reads data from your user, always check the input data right away! We call this process validation, and it’s a regular programming practice.
Here are three ways you can handle invalid input values:
1) Display an error message and end the application.
2) Override the invalid value with a default “good” value and continue with the program.
3) Continue to prompt your user until she enters a valid input.
The best approach depends on the problem. You can also stand behind your user and watch her while she’s typing to make sure it’s 100% correct, but that would be creepy and hard to scale across all your users, so we’ll discuss some other options instead.
You need your user to enter a number between 1 and 5 (inclusive). If she enters a number that’s less than 1 or greater than 5, you’ll prompt her to enter it again, until she gets it right. One way to check if she entered the right number is to use a Goto statement.
TextWindow.Write("Enter a number between 1 and 5: ")
num = TextWindow.ReadNumber()
If ((num < 1) Or (num > 5)) Then
The code checks the input data and, if it’s invalid, it prompts your user again. It works okay if you only have a couple inputs. But if you need to validate many inputs, then using multiple Goto statements can make your program unreadable and turn it into a tangled mess (like Rapunzel’s bad hair day). See Small Basic: Spaghetti Code.
Instead, you can use a While loop, like this:
1 TextWindow.Write("Enter a number between 1 and 5: ")
2 num = TextWindow.ReadNumber()
3 While ((num < 1) Or (num > 5)) ' As long as the data’s invalid
4 TextWindow.Write("Enter a number between 1 and 5: ")
5 num = TextWindow.ReadNumber()
After you prompt your user for data and read her input number (lines 1-2), the While loop checks if the input number’s valid (line 3). If num is outside your range, the loop runs its body (lines 4-5) and asks your user to reenter the data. This loop repeats as long as she enters an invalid number.
Although this code works fine, it repeats the statements to prompt and read data (lines 1-2) inside the loop’s body (lines 4-5).
You can remove the first two statements by forcing your way into the loop’s body, as shown here:
1 ' Using While to to validate an input number
2 num = -1 'Invalid value (to force a pass through the loop)
4 While ((num < 1) Or (num > 5))
5 TextWindow.Write("Enter a number between 1 and 5: ")
6 num = TextWindow.ReadNumber()
8 TextWindow.WriteLine("You entered: " + num)
In line 2, your program sets the variable num to –1 so that the While loop (line 4) is true and runs at least once. Your program prompts your user to enter a number and passes it to the num variable (lines 5-6). The loop’s checked again. If num is below 1 or above 5 (which means she entered an invalid number), the loop’s body runs again, and she’s asked to enter another number. If num is between 1 and 5, the loop ends, and your program moves to line 8 to display the number she entered.
Here’s an example of your user not paying much attention to your instructions, so the question repeats until she enters an acceptable number:
Enter a number between 1 and 5: 0
Enter a number between 1 and 5: 6
Enter a number between 1 and 5: 3
You entered: 3
We showed you three ways to prompt your user until she enters the right answer. Pick the way you feel most comfortable with, but you should know the other ones because you’ll see them when you read other people’s code.
Do you have any questions? Ask us! We're full of answers and other fine things!
Head to the Small Basic forum to get the most answers to your questions:
And go to http://blogs.msdn.com/SmallBasic to download Small Basic and learn all about it!
Small and Basically yours,
- Ninja Ed & Majed Marji