Input from the Keyboard

You have already used the readln procedure to read an item of data into a variable e.g. readln(Surname). You can also use the readln procedure to input multiple items of data of the same type, separated by a space. Procedure MultipleEntry in program ReadFromKeyboard reads three integers and finds their mean.

If you want to read more than a line at a time, read each character as shown in procedure EnterLines. You could try a while not eof do … loop, where the user marks the end of the input with CTRL-Z. However, attempting to mark the end of file in this way will not work if CTRL-Z is intercepted by other software.

program ReadFromKeyboard;
{$APPTYPE CONSOLE}
uses
  SysUtils;

procedure MultipleEntry;
var
  Int1, Int2, Int3 : integer;
  Mean : real;
begin
  write('Please enter three integers separated by spaces. ');
  readln(Int1, Int2, Int3);
  Mean := (Int1 + Int2 + Int3) / 3;
  writeln;
  writeln('Their mean is ', Mean : 6 : 2);
  writeln;
end;

procedure EnterLines;
const
  MAX_CHARS = 1000;
var
  Count, TotalChars : integer;
  Characters : array[1 .. MAX_CHARS] of char;
begin;
  Count := 0;
  writeln('Please enter several lines of text (# then ENTER/RETURN to finish).');
  writeln;
  repeat
    inc (Count);
    read(Characters[Count]);
  until Characters[Count] = '#';
  readln;
  TotalChars := Count - 1;  //Exclude the #
  writeln;
  writeln('ASCII values recorded (with each new line represented by 13 and 10:');
  for Count := 1 to TotalChars do
    begin
      write(ord(Characters[Count]), ' ');
    end;
  writeln;
end;

begin
  MultipleEntry;
  EnterLines;
  readln;
end.
Programming - a skill for life!

Input from keyboard and file and output to monitor, file and printer