Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Pascal - How do you check for EOF when using a keyboard input?

+2 votes
asked Aug 11, 2023 by Gary Ohm (450 points)
Pascal - How do you check for EOF when using a keyboard input?

Turbo Pascal you would do three things: 1) Place a USES CRT statement after the program heading. 2) Set CheckEOF to True with an assignment statement at the start of the program 3) Press Ctrl-Z when done entering data.

I tried that but no luck getting the program to respond to Ctrl-Z when done with data entry.

1 Answer

0 votes
answered Aug 12, 2023 by Peter Minarik (86,240 points)
selected Aug 14, 2023 by Gary Ohm
 
Best answer
<Ctrl>+<Z> is the Windows EOF signal.

However, OnlineGBD is running on a Unix-based system where the end of file signal is <Ctrl>+<D>. See if that works for you.
commented Aug 14, 2023 by Gary Ohm (450 points)
That was a good suggestion but it did not work.
commented Aug 14, 2023 by Peter Minarik (86,240 points)
Ok, I spent some time on this. Here's a small code sample that works:

program EOF_Test;
var s: string;
begin
    repeat
        write('Please, enter some text: ');
        readln(s);
        writeln('You entered: ', s);
    until eof;
    writeln('Bye-bye');
end.

As soon as you add "uses CRT;" it stops working, and EOF (<Ctrl>+<D>) is not recognized anymore.

So if you can go without "uses CRT;", please, do so. Otherwise, try to search the Internet, why this is the case. I am not a Pascal programmer.
commented Aug 14, 2023 by Gary Ohm (450 points)
It turns out I was using a READ statement instead of READLN as in your code. It appears that using the READ statement the reading marker never got past <eoln>. Using the READLN put the reading marker past <eoln> and then was able to read the <eof> keyboard entry (Ctl-D). The USES command looks to be for the PC based program only where the PC needed to get info from a CRT library. OnlineGBD does not have that problem. Thanks for you comments.
commented Aug 14, 2023 by Gary Ohm (450 points)
edited Aug 14, 2023 by Gary Ohm
(* This program shows how to read data until the *)
(* EOF (Ctrl-D) is typed into the keyboard.               *)

Program UsingKeyboard_EOF;

var
  Sysin : Integer;
  
begin
  Writeln('Enter data. When done enter Ctrl-D on the keyboard to stop program');
  
  while not eof do
    begin
      while not eoln do
        begin
          Read(Sysin);
          Write(Sysin, ' ');
        end;
        
      Writeln('End of eoln loop');  { For info only. Status of read data loop }
      Readln;                                        { Set read marker to start of next line    }
    end;
  Writeln('eof reached');
end.
commented Aug 14, 2023 by Peter Minarik (86,240 points)
I'm glad you're sorted. The last time I spent regular time with Pascal was more than 20 years ago, lol.

Keep on coding!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...