
Every program so far has done exactly the same thing every time it ran. A program becomes useful - and a game becomes a game - when it listens. The simplest way to listen is to ask a question and wait for the answer to be typed, and that is what this section is about.
Readln waits for a line to be typed and ENTER to be pressed, and then puts what was typed into a variable:
Write('How old are you? ');
Readln(Age);
The Write puts the question on the screen and leaves the cursor at the end of it. Readln waits there. What is typed appears after the question, and when ENTER is pressed, the number goes into Age and the program carries on from the next line.
Readln reads whatever kind of variable you give it:
Alexandra typed into a string[3] is Ale.It can read more than one number from a single line: Readln(A, B) with 3 4 typed puts 3 in A and 4 in B. The numbers are separated by a space.
If the answer is just ENTER, a number variable is left exactly as it was.
While an answer is being typed, the Einstein's DELETE key does nothing. The keys for correcting an answer are:
| Key | Does |
|---|---|
| CTRL-H, or SHIFT and TAB | Rubs out the last character typed |
| CTRL-X | Rubs out the whole answer, to start again |
That is not a slip. DELETE belongs to Turbo's editor, and in the editor it does what you expect; a running program has its own, simpler way of reading a line, and it wants CTRL-H. In MAME: Ctrl and H.
CAPS LOCK stays as you left it. If you pressed it in the editor to get small letters, the answers you type into your program come out in small letters too; if CAPS LOCK is on, they come out in capitals, and a program that compares them with small-letter text will not find a match.
A program asks for a number and gets twelve:
How old are you? twelve
I/O error 10, PC=006D
Program aborted
Searching
9 lines
Run-time error position found. Press <ESC>
An I/O error is a run-time error in reading or writing - here, in reading a number that is not a number. The program stops. ESC puts you in the editor at the Readln that could not cope.
For now that is simply what happens. A program that must never stop on a bad answer reads it as a string and checks it with Val (S10) first; S12 gives you the tool - if - to act on what Val says.
CTRL-C stops a program that is waiting at a Readln, as S2 said. It is the way out when a program keeps asking and you want to leave.
program Ask;
var
Name: string[20];
Age: Integer;
begin
Write('What is your name? ');
Readln(Name);
Write('How old are you? ');
Readln(Age);
Writeln('Hello, ', Name, '.');
Write('In ten years you will be ');
Writeln(Age + 10, '.');
end.
Starting from
Turbo freshly started, Y, a new work file called ASK. CAPS LOCK pressed once, so that the name comes out as you type it.
What you should see
With Ada and 12 as the answers:
What is your name? Ada
How old are you? 12
Hello, Ada.
In ten years you will be 22.
twelve. What happens, and where does ESC leave you?Write to Writeln. Where does your answer appear now?Name as string[3] and answer with a long name. What does the program call you?11.1 Ask for two numbers on one line and print their sum.
11.2 Ask for a temperature in Fahrenheit and print it in Celsius, to one decimal place. (S9's exercise 9.3 has the sum.)
11.3 Ask for a word, and print how many letters it has and its first letter as a capital.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
| DELETE does nothing while answering | It does not, at a program's question. Use CTRL-H, or SHIFT and TAB. |
I/O error 10 | The answer to a question wanting a number was not a number. |
| The answer appears on the line below the question | The question was printed with Writeln. Use Write. |
| The answer comes out in capitals | CAPS LOCK is on. |
| A name comes out cut short | The string variable is too short for it. |
| The program waits and nothing happens | It is at a Readln, waiting for ENTER. CTRL-C stops it. |
Write a question, then Readln a variable: the program waits for a line and ENTER, and puts the answer in the variable - a number or a string. Correct an answer with CTRL-H, not DELETE; clear it with CTRL-X. A word where a number was wanted is I/O error 10, and stops the program.
S12, Making A Choice - if, and doing one thing or another depending on the answer.