Module 4A COMPUTER PROGRAMMING 2
Module 4A COMPUTER PROGRAMMING 2
Chapter 7:
Input Validation
7.1 Garbage In, Garbage 7.2 The Input Validation 7.3 Defensive
Out Loop Programming
7.1 Garbage In, Garbage Out
CONCEPT: If a program reads bad data as input, it will produce bad data as output.
Programs should be designed to reject bad data that is given as input.
If a program reads bad data as input, it will produce bad data as output
Programs should be designed to accept only good data
❖ Program 7-1 Payroll program, notice what happens in the sample run when the user
gives bad data as input.
7.2 The Input Validation Loop
CONCEPT: Input validation is commonly done with a loop that iterates as long as an
input variable contains bad data.
➢ All input should be inspected before processing
➢ If it’s invalid, it should be rejected and the user should be prompted to enter the
correct data
❖ Figure 7-1 shows a common technique for validating an item of input.
• The input is read, and then a pretest loop is executed.
• If the input data is invalid, the body of the loop executes.
• The loop displays an error message so the user will know that the input was
invalid, and then the loop reads the new input.
• The loop repeats as long as the input is invalid.
In Figure 7-1 reads input in two places: first just before the loop and then inside the loop.
• The first input operation—just before the loop—is called a priming read, and its
purpose is to get the first input value that will be tested by the validation loop. If
that value is invalid, the loop will perform subsequent input operations.
❖ Example. Suppose you are designing a program that reads a test score and you want to
make sure the user does not enter a value less than 0. The following pseudocode shows
how you can use an input validation loop to reject any input value that is less than 0.
// Get a test score.
Display "Enter a test score."
Input score
The loop in this pseudocode determines whether score is less than 0 or greater than 100. If
either is true, an error message is displayed and the user is prompted to enter a correct score.
Although this logic will work, it does not display an error message when the user enters an
invalid value—it simply repeats the original prompt each time the loop iterates. This might be
confusing to the user, so it is usually better to have a priming read followed by a pretest
validation loop.
The validation loop uses a long compound Boolean expression that will iterate as long as model
does not equal 100 AND model does not equal 200 AND model does not equal 300. Although
this logic will work, you can simplify the validation loop by writing a Boolean function to test the
model variable and then calling that function in the loop.
For example, suppose you pass the model variable to a function you write named isInvalid. The
function returns True if model is invalid, or False otherwise. You could rewrite the validation
loop as follows:
This makes the loop easier to read. It is evident now that the loop iterates as long as model is
invalid. The following pseudocode shows how you might design the isInvalid function. It accepts
a model number as an argument, and if the argument is not 100 AND the argument is not 200
AND the argument is not 300, the function returns True to indicate that it is invalid. Otherwise,
the function returns False.
o This input validation loop rejects any input except the strings "yes" and "no".
o This particular design might be too rigid, however; as it is written, the loop performs a
case-sensitive comparison. This means that strings such as "YES", "NO", "Yes", and "NO"
will be rejected.
o To make the program more convenient for users, the program should accept "yes" and
"no" written in any combination of upper- or lowercase letters.
Library functions such as toUpper and toLower can help make case-insensitive string
comparisons. The following pseudocode shows an example using the toLower function.
Sometimes the length of a string plays a role in the string’s validity.
❖ For example, you have probably used a Web site or other system that required you to
set up a password. Some systems require that passwords have a minimum number of
characters.
In the following pseudocode, the length function is used to make sure a password is at
least six characters long.