Interactive I/O in Pascal

Charles Hedrick root at topaz.RUTGERS.EDU
Wed Jul 30 10:06:06 AEST 1986


You are right that I have heard the complaint about interactive I/O a
number of times in the past.  However it has appeared much less often
recently, since the various implementations seem to be converging on
the use of "lazy I/O".  Most implementors seem to believe that the ISO
standard implies that this is the correct way to handle interactive
I/O.  You complain that the following program will print an extra
prompt.

	writeln ('enter an integer : ');
	while not eof do
	   begin
	      readln (i);
	      writeln ('enter an integer : ');
	   end;

As I see it, you will have roughly the following dialog:

  enter an integer: 123
  enter an integer: ^D

at which point the program is finished.  I don't see what is wrong
with that.  Perhaps you want

  enter an integer: 123^D

If you want to be able to recognize that, you will simply have to
use a separate call to readln.  Presuming that you want to be able
to recognize ^D in either context, the program would look like

	while true do 
	   begin
	   writeln ('enter an integer : ');
	   if eof 
	     then {exitloop}				goto 666;
	   read (i);
	   if eof
	     then {exitloop}				goto 666;
	   readln;
	   end;						666:

This tests for eof twice because I assume you want to allow it either
place. (Sorry about the goto's, but the logic is clearer that way than
with any alternative I could think of.)

As far as I know, the semantics of lazy I/O allow you to check for any
possible input, printing prompts at any point.  This does not mean
that Pascal's input parsing is as powerful as scanf, or that its
output is as powerful as printf.  Obviously they are not.  Validating
input, to make sure that the user hasn't typed something bogus, can
require a bit more programming in Pascal than in C.  For example, in
the above programs, if the user types an extra CR before the ^D the
program may think that there is one more number than there is.  If you
want the program to be very careful about checking validity of its
input, you will need to write code to check that the user has typed
one and exactly one number on each line.  But as far as I know, you
can always find a way to avoid having extra prompts and misplaced
input hangs.



More information about the Comp.lang.c mailing list