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 space

then wait for you to enter in the two numbers. If you typed the two numbers, then pressed the return key, eg,


	 237  64<return key press>

then 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


Differences between READ and READLN
The readln statement discards all other values on the same line, but read does not. In the previous program, replacing the read statements with readln and using the same input, the program would assign 237 to numb1, discard the value 64, and wait for the user to enter in another value which it would then assign to numb2.

The <return key> is read as a blank by read, and ignored by readln.


SELF TEST 6: 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 7: 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, 1988-1999. All rights reserved.