HiSoft Pascal Primer cover: a square, a triangle and a circle drawn and filled by a HiSoft Pascal program on the Tatung Einstein
← Back to Courses
module
9

Asking A Question

Introduction

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

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.

Letters, And The Empty Line

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.

The Code

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

Change One Thing

  • Run it again and type 12, ENTER, 30, ENTER. Does it still add them?
  • Run it and type 12 X. What stops it, and which line does the address point to?
  • Take out line 80, compile, and answer 12 30 and then Y. Does the program wait for your Y? What does it answer?

Exercises

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.

When It Goes Wrong

SymptomCause
A letter test always failsThe 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 emptyNo READLN before it: it read the rest of the last line.

Summary

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.

Next

S10, Choices, Loops And Procedures - three small differences, and one large improvement on Turbo.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.