GETTING INFORMATION/DATA FROM THE KEYBOARD INTO A PROGRAM
It is convenient to accept data whilst a program is running.
The read and readln statements allow you to read
values and characters from the keyboard, placing them directly
into specified variables.
The program which follows reads two numbers from the keyboard, assigns them to the specified variables, then prints them to the console screen.
program READDEMO (input, output); var numb1, numb2 : integer; begin writeln('Please enter two numbers separated by a space'); read( numb1 ); read( numb2 ); writeln; writeln('Numb1 is ', numb1 , ' Numb2 is ', numb2 ) end.When run, the program will display the message
Please enter two numbers separated by a spacethen wait for you to enter in the two numbers. If you typed the two numbers, then pressed the return key, eg,
237 64then the program will accept the two numbers, assign the value 237 to numb1 and the value 64 to numb2, then continue and finally print
Numb1 is 237 Numb2 is 64
The
SELF TEST on READ
Assuming that we made the following declaration
var C1, C2, C3, C4, C5, C6 : char;
and that the user types
ABCDE
then what would each of the following statements assign to the
various variables,
read( C1 ); C1 = __
read( C2 ); read( C3 ); C2 = __ C3 = __
read( C4, C5, C6 ); C4 = __ C5 = __ C6 = __
Click here for answer
SELF TEST on READLN
Assuming that we made the following declaration
var C1, C2, C3, C4, C5, C6 : char;
and that the user types
ABCDE
FOR EACH LINE, then what would each of the following statements
assign to the various variables,
readln( C1 ); C1 = __
readln( C2 ); readln( C3 ); C2 = __ C3 = __
readln( C4, C5, C6 ); C4 = __ C5 = __ C6 = __
readln; _________________________
Click here for answer
Copyright B Brown/P Henry/CIT, 1988-1997. All rights reserved.