
In the Turbo course, Readln(A) waited for a line, and took a number from it. HiSoft Pascal's READ and READLN look the same and mostly are - but every HiSoft program starts with an empty line already typed, and until you know that, reading a single letter gives answers that make no sense.
Numbers are the easy case. READ(A,B) waits for you to type, and takes two numbers - on one line with a space between, or on two lines, it does not mind. Reals read the same way. Line ends and spaces before a number are skipped, which is why numbers work without any fuss.
Type letters where a number should be, and the program stops:
Number expected at PC=0EA2
where Turbo gave I/O error 10.
While you type, DELETE rubs out the last character - better than Turbo's Readln, where the Einstein's DELETE key did nothing.
READ(C) for a CHAR takes one character from what has been typed - including the end of a line, which is a character too, CHR(13).
And the keyboard's input starts, as the manual puts it, with an empty line. So the very first READ(C) of a program waits for you to type a line - and then hands you CHR(13), the end of that first empty line, not your letter. The same happens after reading numbers: the end of the numbers' line is still waiting to be read.
The cure is one statement. READLN moves on to a fresh line - it throws away whatever is left of the current one and waits for the next. Put a READLN before every READ of a character or a word (S8), and they work as you would expect:
WRITE('Y OR N',CHR(63),' ');
READLN;
READ(C);
Numbers do not need it - but it does no harm.
10 PROGRAM ASK;
20 VAR A,B:INTEGER; C:CHAR;
30 BEGIN
40 WRITE('TWO NUMBERS',CHR(63),' ');
50 READ(A,B);
60 WRITELN('SUM ',A+B);
70 WRITE('Y OR N',CHR(63),' ');
80 READLN;
90 READ(C);
100 IF C='Y' THEN WRITELN('YES') ELSE WRITELN('NO')
110 END.
Starting from
ASK.ASC saved from XBAS; HPEIN ASK.
What you should see
Type 12 30, ENTER, then Y, ENTER:
TWO NUMBERS? 12 30
SUM 42
Y OR N? Y
YES
12, ENTER, 30, ENTER. Does it still add them?12 X. What stops it, and which line does the address point to?12 30 and then Y. Does the program wait for your Y? What does it answer?9.1 Ask for a number of seconds and print it as minutes and seconds.
9.2 Ask for a word (S8) and a number n, and print the word n times.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
| A letter test always fails | The READ got the end of a line. Put READLN before it. |
Number expected at PC=... | Something other than a number was typed for a number. |
| A word read in is empty | No READLN before it: it read the rest of the last line. |
READ of numbers skips spaces and line ends, and stops the program with Number expected at anything else. READ of a character or a word takes exactly what is next - often the end of a line, because input starts with an empty line. Put READLN before every READ of letters. DELETE works while you type.
S10, Choices, Loops And Procedures - three small differences, and one large improvement on Turbo.