Learn Pascal
2A - Input

Input means to read data into memory, either from the keyboard, the mouse, or a file on disk.

We will not get into mouse input in detail, because that syntax differs from machine to machine, and may require a complicated interrupt call to the mouse driver. If you would like to see the source code for a mouse support unit for DOS, click here. This unit will not work under Windows, Macintosh, or X-Windows, because these operating systems handle all mouse input for you and do not let you interface with the mouse directly.

The basic format for reading in data is:
     read (Variable_List);
Variable_List is a series of variable identifiers separated by commas.

read, however, does not go to the next line. This can be a problem with character input, because the end-of-line character is read as a space.

To read data and then go on to the next line, use
     readln (Variable_List);

Suppose you had this input from the user, and a, b, c, and d were all integers.
     45 97 3
     1 2 3

This would be the result of various statements:

Statement(s) a b c d
read (a);
read (b);
4597
readln (a);
read (b);
451
read (a, b, c, d);45 9731
readln (a, b);
readln (c, d);
459712

You see, the read statement does not skip to the next line unless necessary, whereas the readln statement is just a read statement that skips to the next line at the end of reading.

When reading in integers, all spaces are skipped until a numeral is found. Then all subsequent numberals are read, until a non-numeric character is reached (including, but not limited to, a space).
     8352.38
When an integer is read, its value becomes 8352.
If, immediately afterwards, you read in a character, the value would be '.'

Suppose you tried to read in two integers. That would not work, because when the computer looks for data to fill the second variable, it sees the '.' and stops, saying, "I couldn't find any data to read."

With real values, the computer also skips spaces and then reads as much as can be read. However, there is one restriction: a real that has no whole part must begin with 0.
     .678
is invalid, and the computer can't read in a real, but
     0.678
is fine.

Make sure that all identifiers in the argument list refer to variables! Constants cannot be assigned a value, and neither can literal values.


To solution

Next lesson

Contents

Index

e-mail me


taoyue@mit.edu
Copyright © 1997-2001 Tao Yue. All rights reserved.