[fpc-pascal]problems with while not eoln (input)

Jonas Maebe jonas at zeus.rug.ac.be
Fri May 3 13:15:16 CEST 2002


On Fri, 3 May 2002, Morten Gulbrandsen wrote:

You forgot an important part:

   writeln(output, 'TYPE SOME CHARACTERS AND ');
   writeln(output, 'TERMINATE A STRING WITH A  *');
   repeat

      ind := 0;
      if eoln(input) then readln(input);

Here, when you haven't typed anything yet, the program will wait for
input. The reason is that eoln() doesn't know whether it's the end of the
line if there is no input (since the next character might or might not be
a return). Further, the input is line buffered. This means that

a) you call eoln()
b) eoln will detect that the input buffer is empty and will call a
procedure to read a line, which will read all characters from the
keyboard until the first "enter" (that's what line buffering means)
c) after you press enter, eoln will start scanning the input buffer. It
will find the enter and return true.
d) since eoln returned true, readln is called, which will read/remove all
characters from the input buffer until the first enter. So the input
buffer is empty again and you're back at square one.

>     while not eoln(input)
>             do begin
>                read(input, ch);

So here, the first time the input buffer is empty and we wait again for
input, which explains why you have to press enter twice.

>                if not eoln(input)
>                   then begin
>
>                      ind := ind + 1;
>                      line[ind] := ch;
>
>                   end;
>             end;
>
>
> Can I modify eoln with some compiler option ?

No. It's just a procedure in the system unit, it's not a special language
construct or so.

> complete sample program is attached.
>
> It is from a textbook written for UCSD  Apple Pascal
> I think apple has  \n\r\f  as cr   windows has \n\r   and linux has only
> \n
> as cr, but I don't know if this is relevant.

Mac uses \r, but that doesn't matter indeed , that's all handled
transparently.


Jonas





More information about the fpc-pascal mailing list