Date : Thu, 14 Feb 1985 09:28:13 EST (Thursday)
From : Bob <Slomcenski.wbst@XEROX.ARPA>
Subject: Re: Problem reading while not eoln in Turbo Pascal
Dave,
One alternative to the character - read problem that I have used (I
use Turbo on an 8 bit Z80 -CP/M system) is the following:
{ Function InKey - Returns a keystroke from the keyboard }
{ returns a keyboard character if one is present, else returns ASCII
NUL }
function InKey: char;
var ch: char;
begin
if KeyPressed then Read(Kbd,ch) else ch:= chr(0);
InKey:= ch;
end;
Using this procedure, you can get (and process) any keystroke from the
keyboard. You must provide the explicit processing for the character,
ie: you can't use eoln.
This function emulates the BASIC INKEY$ function that is available in
most of the BASICs now in use. It does not echo to the CRT, and if no
key has been pressed, it returns ASCII NUL.
I would use it in your code as follows:
> var
> Index: integer;
> InChar: char;
> Digits: array [1..5] of integer;
>
> begin
> { lines to request user input }
> Index:= 0;
> InChar:= #0;
> while ((Index < 5) and (InChar <> #13)
> do begin
> InChar:= InKey;
> case InChar of
> #0, #13: begin end;
> '0'..'7': begin
> Index:= Index + 1;
> Digits[Index]:= (ord(InChar) -
ord('0'));
> write(InChar); { echo character }
> end;
> otherwise: begin
> { invalid character routine }
> end;
> end; { while }
>
~ Bob