1
votes

Getting started with Pascal here. I wanted to write a simple program that first clears the terminal window and then reads user input. The first result of searching for clearing the screen showed the ClrScr procedure. Using it gets the job done but ClrScr needs Crt which results in a new problem. Terminating the program using Ctrl + C does not work. Searching online again, I found that Crt takes over I/O. I have been looking for alternative to ClrScr but haven't really found anything so far.

So how can I clear terminal while still being able to terminate the program using Ctrl + C. Also how can I terminate the program in the current case using Crt?

Current Code:

    program Test;
    uses Crt;
    var     
            x : integer;
    begin
            read(x);
    end.
1
What operating system are you using? Linux and Windows have a command to clear the screen, so you could make a system call to that command. The other option, while using Crt, is to catch Ctrl+C by reading it as a key and acting on it explicitly. - lurker
@lurker macOS sierra - user4447799
You will have to explicitly read ^C as a key (using KeyPressed, ReadKey) at appropriate places and react to it. On Windows, there is a SysSetCtrlBreakHandler, but I don't know if that exists on other platforms. - Rudy Velthuis
@RudyVelthuis So far, reading ^C seems the only way to do it and something that I don't feel like implementing. Might just discard Crt from the code totally. - user4447799
Yes, discarding Crt is not such a problem. If you need coloured text or something like ClrScr, simply use the appropriate OS routines, or use escape sequences (IIRC, that should work on the Mac too). - Rudy Velthuis

1 Answers

1
votes

So far the solutions I have seen online and the ones suggested in the comments for keeping CRT just make it troublesome and unnecessary makes the program complex. So for now I have decided just to discard it totally.

The workaround I have found now is to use unix.fpsystem with the clear command which gets the job done just fine.