12 Reprompting
Programs that use the console (sometimes called command-line programs) frequently need to prompt the user to provide input, validate the input, and reprompt if the input is invalid. There are many different ways to accomplish these tasks, but, as with all of the problems discussed thus far, there are better and worse ways to do so.
Motivation
Suppose you must write a program that prompts the user to enter their age and then retrieves their response. Since the user might make a mistake and enter a negative value, your program must check the user’s response and reprompt them if they’ve made a mistake.
The phrase “if they’ve made a mistake” might lead you to use an if
statement to deal with this situation. In other words, you might be led to think the solution is something like the following (in pseudocode):
Prompt the user to enter their age; Read the user's response; Assign the response to the variable named age; if (age is negative) { Prompt the user to enter their age; Read the user's response; Assign the response to the variable named age; } Use the variable named age;
You would then test your program, see it works correctly, and deploy it. Unfortunately, at some point, the user will probably enter an invalid response to both prompts, and the program will fail (in one way or another).
Because beginning programmers tend to get locked into a particular solution, you might then “correct” the code as follows:
Prompt the user to enter their age; Read the user's response; Assign the response to the variable named age; if (age is negative) { Prompt the user to enter their age; Read the user's response; Assign the response to the variable named age; if (age is negative) { Prompt the user to enter their age; Read the response; Assign the response to the variable named age; } } Use the variable named age;
Of course, this doesn’t correct the defect at all. That is, this code will only work for two or fewer invalid responses, and there is no limit on the number of times that the user can enter an invalid response.
Review
What you need is a way to repeat a block of code an indefinite number of times (i.e., until the user enters a valid response). The phrase “repeat a block of code” should immediately bring to mind a loop of some kind. The phrase “an indefinite number of times” should bring to mind a while
or do
loop. In other words, you are already partway to a solution.
As you know, a while
loop is appropriate if the body needn’t be executed, and a do
loop is appropriate if the body must be executed at least once. So, you might be thinking that the solution to the reprompting problem involves a while
loop since you don’t always need to reprompt. The truth is slightly more complicated than that.
Thinking About The Problem
It turns out that there are, in fact, two different versions of this problem. In the first, the initial prompt and the subsequent prompts (i.e., after the user provides an invalid response) are the same. In the second, the i