<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Mon, 11 Feb 1985 18:49:49 GMT
From   : Dave Beyerl <db21%ihuxk.uucp@BRL-TGR.ARPA>
Subject: Re: Problem reading while not eoln in Turbo Pascal

> 
>   I am trying to implement a procedure similar to the following
> that will read from the CONSOLE until end-of-line is reached;
> 
>   procedure ReadLine;
>   var InChar: char;
>   begin
>        while not eoln 
>             do begin
>                          read (InChar);
>                  (do something with InChar)
>                   end;
>              writeln { close current line }
>           end;
> 
>   In section 14.5.2 (pg 101) of the Turbo manual, it states that
> you cannot look ahead on a logical device so Eoln and Eof operate on
> the 'last' character read instead of the 'next' character to read.  The
> effect then is that Eoln returns true when the last character read was
> a CR or a Cntl-Z.  This seems reasonable and should not be too
> difficult to implement.  Well, I don't seem to be having much luck in
> getting my procedure to work.
> 
>   The function of the procedure I have written is to input a
> string of characters which represent a base 8 (octal) number.  In
> part my code is as follows:
> 
>   var
>        Index: integer;
>        InChar: char;
>        Digits: array [1..5] of integer;
> 
>   begin
>        { lines to request user input }
>        Index:= 0;
>              read (InChar); { is next char CR or Cntl-Z? }
>              while ((not eoln) and (Index < 5))
>                   do begin
>                        if (InChar in ['0'..'7'])
>                        then begin
>                             Index:= Index + 1;
>                             Digits[Index]:= (ord(InChar) - ord('0'));
>                             read (InChar); { see what next char is }
>                             end { then }
>                        else begin
>                             { invalid character routine }
>                             end; { else }
>                    end; { while }
> 
>   When I run the program containing this segment, it requests
> I enter an octal number.  I enter a string such as 4174 and hit
> return.  Nothing happens.  In fact nothing happens until I hit
> return "twice" more.  The program continues processing and returns
> a result that shows it read ONLY THE FIRST character of the input
> string.
> 
>   I suspect the problem is in the way that input from a
> logical device is buffered, but I may be missing a subtle point 
> of either Pascal or Turbo or both.  Has anyone else experienced
> this problem, and if so how did you work around it?  
>   I am running Turbo on a Z80 based micro with CP/M
> operating system.  Comments and suggestions are welcome 
> - please respond by mail.  Thanks in advance.
> 
> 
> For every problem there is one            Dave Beyerl
> solution which is simple, neat,                 ihuxk!db21
> and wrong!

       I have since been in touch with the Technical Assistance
group at Borland on the above problem.  They told me that anything
beyond the first character is lost when doing a character read from
a logical device <files are still handled as they should be>.  They
are not sure why this is but they are looking into it.  They believe
Turbo is this way because it has character handling capabilities not
present is standard Pascals.  
       For a solution to the problem, they suggested reading the 
input character string into a variable declared as string.  Once the 
string has been entered, it can be parsed and manipulated as needed 
using the string procedures and functions described in section C.5 
of the manual.  So, for now at least, the word is BEWARE when reading
from logical devices - your input is not processed the same as
when reading from files!
                                       Dave Beyerl
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>