Input loop is part of Short Circuit's Console Program Basics selection.
Task

Read from a text stream either word-by-word or line-by-line until the stream runs out of data.

The stream will have an unknown amount of data on it.




PROCEDURE ReadName (VAR str : ARRAY OF CHAR);
 
VAR     n               : CARDINAL;
        ch, endch       : CHAR;
 
BEGIN
   REPEAT
      InOut.Read (ch);
      Exhausted := InOut.EOF ();
      IF  Exhausted  THEN  RETURN  END
   UNTIL  ch > ' ';             (* Eliminate whitespace         *)
   IF  ch = '['  THEN  endch := ']'  ELSE  endch := ch  END;
   n := 0;
   REPEAT
      InOut.Read (ch);
      Exhausted := InOut.EOF ();
      IF  Exhausted  THEN  RETURN  END;
      IF  n <= HIGH (str)  THEN  str [n] := ch  ELSE  ch := endch  END;
      INC (n)
   UNTIL ch = endch;
   IF  n <= HIGH (str)  THEN  str [n-1] := 0C  END;
   lastCh := ch
END ReadName;