If you haven't done so already, run the program while giving deliberately bad
input from the human. Try giving:
- A wrong pile number
- A pile with zero coins
- A wrong number of coins
- Input that is not even numerical
In this step you will change your program so that it insists on valid input.
When working correctly your program should behave as shown to the right.
The best solution is to modify
the
prompt procedure so that it takes a procedure as a
second parameter that can be applied to the user input for validity.
A third parameter specifies the string to display when the input is invalid:
(define prompt
(lambda (prompt-string valid? error-string) ; note added parameters
...))
When the input fails the test, the error string should be displayed and the
original prompt should be repeated (use iteration).
To test for non-numerical input, use the built-in
integer? predicate,
which takes an argument and tests for whether it is an integer.
Here is an example of how it should work, with user input in
red:
> (prompt "Please enter an integer: "
(lambda (x) (integer? x))
"Not an integer. Try again.")
Please enter an integer:
3.14159
Not an integer. Try again.
Please enter an integer:
foo
Not an integer. Try again.
Please enter an integer:
13
13
>