Looping Programs - Data Validation
Overview:
Programs often require user input to be useful. Unfortunately, you can't depend on the user to always input logical (valid) data. For instance, if a program asks how old the user is, you'll probably get pretty wierd results if they enter 500 years old or -5. A good programmer VALIDATES the data the user enters by ensuring that the program doesn't move forward until the users enters something useful.
Example:
The following code asks the user to enter an age, but keeps looping until the user enters a value that is more than 0 and less than 130.
| DO |
|
| |
PRINT "Please enter your age (must be 1 - 130)"
INPUT age |
| LOOP UNTIL age > 0 AND age < 130 |
Assignment:
Create a new folder in your qbasic folder called validity. Save all of the following programs in that folder.
For each program listed, use data validity loops to make sure that the user doesn't enter data that would not fit the needs of the program.
- Write a short program that asks the user to guess a number between 1 and 100. Use a data validity loop to make sure that their guess is within range. Save the program in the validity folder as loops1.
- Write a short program that asks the user what grade they are in. The program is intended to be used by high school students only. Validate data and save the program as loops2.
- Write a short program that asks the user to enter a word that ends with the letter 'S'. Validate data and save the program as loops3.
- Write a program that asks the user to enter a 6 letter word. Validate data and save the program as loops4.
- Write a program that asks the user to enter a three letter word that uses the letter 'A'. Validate data and save the program as loops5.
- Write a program that asks the user to enter a negative number. Validate data and save the program as loops6.
- Write a program that asks the user to enter four letter word that starts with the letter 'M'. Validate data and save the program as loops7.