Turbo Pascal 2.0 course cover
← Back to Courses
module
11

Asking A Question

Introduction

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

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:

  • An integer or a real: the answer must be a number, and the variable gets its value.
  • A string: the variable gets the whole line, as typed. If it is longer than the string can hold, it is cut, as in S10 - 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.

Correcting An Answer

While an answer is being typed, the Einstein's DELETE key does nothing. The keys for correcting an answer are:

KeyDoes
CTRL-H, or SHIFT and TABRubs out the last character typed
CTRL-XRubs 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.

Capitals In Answers

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.

When The Answer Is Wrong

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.

The Code

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.

Change One Thing

  • Run it and answer the age with a word, twelve. What happens, and where does ESC leave you?
  • Change the first Write to Writeln. Where does your answer appear now?
  • Declare Name as string[3] and answer with a long name. What does the program call you?

Exercises

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.

When It Goes Wrong

SymptomCause
DELETE does nothing while answeringIt does not, at a program's question. Use CTRL-H, or SHIFT and TAB.
I/O error 10The answer to a question wanting a number was not a number.
The answer appears on the line below the questionThe question was printed with Writeln. Use Write.
The answer comes out in capitalsCAPS LOCK is on.
A name comes out cut shortThe string variable is too short for it.
The program waits and nothing happensIt is at a Readln, waiting for ENTER. CTRL-C stops it.

Summary

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.

Next

S12, Making A Choice - if, and doing one thing or another depending on the answer.

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.