I am studying coding in pascal for an upcoming exam. My computer runs the free pascal compiler and uses Geany as an IDE. My Mac Book Pro runs on macOS Sonoma and the terminal is zhs.
Practicing the eol and eof functions, I programmed the following frequency counter (an example from my textbook). The program asks one to enter a letter. Then in starts reading whatever one types from the keyboard. When one presses CTRL-Z the program is supposed to end and displays how many times the specified letter was repeated in the file.
PROGRAM FreqCounter1(INPUT,OUTPUT);
uses crt;
VAR
Ch, SpecificChar :CHAR;
Counter, FreqCounter :INTEGER;
BEGIN
Counter := 0;
FreqCounter := 0;
WRITE('Enter the required letter: ');
READLN(SpecificChar);
WRITELN('Start typing. Press Ctrl-Z to finish.');
WHILE NOT EOF DO
BEGIN
WHILE NOT EOLN DO
BEGIN
READ(Ch);
IF (Ch >= 'A') AND (Ch <= 'Z') OR (Ch >= 'a') AND (Ch <= 'z') THEN
Counter := Counter + 1;
IF Ch = SpecificChar THEN Freqcounter := FreqCounter + 1;
END;
READLN
END;
WRITELN('Total number of letters= ', Counter);
WRITELN('The letter ''', SpecificChar, ''' was repeated ', FreqCounter, ' time(s)');
WRITELN('Frequency of repetition= ', freqCounter/Counter*100:2:2,'%')
END.```
As the picture shows, I am able to enter a specific character and write two test sentences. When I press CTRL-z to end the input the program is stuck.
[Terminal Window](https://i.sstatic.net/oC9trA4i.png)
As I mentioned above the program gets stuck. After specifying a character and writing two sentences as input, the program doesn't continue.
In order to end the input through a EOF marker, I tried various keyboard shortcuts:
- ctrl-d + enter
- ctrl-d after the last character + enter
- ctrl-d on a new line and enter
- ctrl-d + ctrl-d on a new line and enter
- ctrl-q + ctrl-d on a new line and enter
- ctrl-z after the last character and enter
- ctrl-z on a new line and enter and enter
- ctrl-z + ctrl-z + enter
- ctrl-v + enter
- ctrl-t + enter
- Holding the ctrl key, I tried the whole alphabet, without success.
Searching stackoverflow and using the help of the articles I put the command stty all in a shell and got the following output in the terminal window (see picture).
[enter image description here](https://i.sstatic.net/3zIDfnlD.png)
I also ran the frequency counter program on replit and I am confronted with the same problem. I am unable to enter EOF and end the input. So the program gets stuck again.
I did change the program so that the character # would end the input. It worked.
Since I will be tested on both the eoln and eof functions where I have to code a program using both functions, I am trying to make the original program work. I did ask my instructors, but they told me to use ctrl-d or ctrl-z.
If someone could help me that would be great. I've spend a lot of time on this matter already.
Niev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.