Recent

Author Topic: [SOLVED]Program experiencing runtime error, not displaying expected output.  (Read 559 times)

Cainnech

  • Newbie
  • Posts: 2
Hello, guys.
I am using the latest free pascal compiler and I am on windows 11.
 
I recently got my hands on an old pascal textbook, and I have been trying to learn from it since I have no experience with pascal and my programming skills and problem solving skills aren't great at all (they are really bad), so I wanted to go back to basics to build up a solid foundation this time.

I have encountered a problem trying to replicate one of the examples, the other examples work well but this one doesn't. It compiles but when I try to insert data, it doesn't output anything.

It's supposed to work like this:
INPUT: baa, baa, black sheep.
OUTPUT: 16 Characters, 3 blanks, 2 commas, 1 period

This is the program:
Code: Pascal  [Select][+][-]
  1.  program countchars(input, output);
  2. {$MODE ISO}
  3.   const
  4.     blank = ' ';
  5.     comma = ',';
  6.     period = '.';
  7.  
  8.   var
  9.     charcount, blankcount, commacount, periodcount: integer;
  10.     character : char;
  11.   begin
  12.      charcount := 0;
  13.      commacount := 0;
  14.      blankcount := 0;
  15.      periodcount := 0;
  16.      while not eof do
  17.         begin
  18.            read(character);
  19.            charcount := charcount + 1;
  20.            if character = blank
  21.              then blankcount := blankcount + 1
  22.            else if character = comma
  23.              then commacount := commacount + 1
  24.            else if character = period
  25.              then periodcount := periodcount + 1
  26.         end;
  27.     writeln (charcount, 'characters');
  28.     writeln (blankcount, 'blanks');
  29.     writeln (periodcount, 'periods');
  30.     writeln (commacount, 'commas');
  31.   end.
  32.  
  33.  

I am not sure what to do, or what the problem is.

I also encountered another issue, when I tried replacing the "char" type with a "string" type, it returned exit code 201, Apparently this is a range check error and it's trying to store a value where it doesn't fit. I would like to know why, because isn't the string type bigger than the char type, so why does it crash whenever I try storing strings in the string type.

Thank you in advance,
Kenneth.
« Last Edit: June 13, 2026, 12:20:09 am by Cainnech »

Handoko

  • Hero Member
  • *****
  • Posts: 5559
  • My goal: build my own game engine using Lazarus
Re: Program experiencing runtime error, not displaying expected output.
« Reply #1 on: June 12, 2026, 05:26:59 pm »
@Cainnech

I try to change as little as possible from your code to make it works:

Code: Pascal  [Select][+][-]
  1. program countchars(input, output);
  2.  
  3. uses
  4.   Types;
  5.  
  6. {$MODE ISO}
  7.  
  8.  
  9.   const
  10.     blank = ' ';
  11.     comma = ',';
  12.     period = '.';
  13.  
  14.   var
  15.     charcount, blankcount, commacount, periodcount: integer;
  16.     characters : string;
  17.     character: char;
  18.     i: byte;
  19.   begin
  20.      charcount := 0;
  21.      commacount := 0;
  22.      blankcount := 0;
  23.      periodcount := 0;
  24.      ReadLn(characters);
  25.      for i := 0 to Length(characters)-1 do
  26.      begin
  27.        character := characters[i+1];
  28.        charcount := charcount + 1;
  29.        if characters[i+1] = blank
  30.          then blankcount := blankcount + 1
  31.        else if character = comma
  32.          then commacount := commacount + 1
  33.        else if character = period
  34.          then periodcount := periodcount + 1
  35.     end;
  36.     writeln (charcount, 'characters');
  37.     writeln (blankcount, 'blanks');
  38.     writeln (periodcount, 'periods');
  39.     writeln (commacount, 'commas');
  40.   end.

Cainnech

  • Newbie
  • Posts: 2
Re: Program experiencing runtime error, not displaying expected output.
« Reply #2 on: June 13, 2026, 12:19:17 am »
Thank you so much, it works perfectly.

Zvoni

  • Hero Member
  • *****
  • Posts: 3444
Re: Program experiencing runtime error, not displaying expected output.
« Reply #3 on: June 15, 2026, 10:32:39 am »
Thank you so much, it works perfectly.
I disagree.
Handoko's code returns for charcount the length of the String
Code: Pascal  [Select][+][-]
  1. program countchars(input, output);
  2. uses Types;
  3. {$MODE ISO}
  4. const
  5.     blank = ' ';
  6.     comma = ',';
  7.     period = '.';
  8.  
  9.   var
  10.     charcount, blankcount, commacount, periodcount: integer;
  11.     characters : string;
  12.     character: char;
  13.     i: byte;
  14.   begin
  15.     charcount := 0;
  16.     commacount := 0;
  17.     blankcount := 0;
  18.     periodcount := 0;
  19.     ReadLn(characters);
  20.     for i := 1 to Length(characters) do
  21.     begin
  22.        Case characters[i] Of
  23.          blank : blankcount := blankcount + 1;
  24.          comma : commacount := commacount + 1;
  25.          period: periodcount := periodcount + 1;
  26.        end;
  27.     end;
  28.     charcount:=Length(characters)-(blankcount+commacount+periodcount);
  29.     writeln (charcount, ' characters');
  30.     writeln (blankcount, ' blanks');
  31.     writeln (periodcount, ' periods');
  32.     writeln (commacount, ' commas');
  33.     Readln;
  34.   end.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12959
  • FPC developer.
If you want to keep it strictly ISO, try replacing EOF with EOLN

tetrastes

  • Hero Member
  • *****
  • Posts: 769
Quote
It's supposed to work like this:
INPUT: baa, baa, black sheep.

INPUT: baa, baa, black sheep.EOF (windows Ctrl+Z, unix usually Ctrl+D)

Thaddy

  • Hero Member
  • *****
  • Posts: 19498
  • Glad to be alive.
Marcov is right, EOLN is correct. EOF means that input -a.k.a. stdin - reads end of file, which it isn't. EOF depends on programming style. (and dialect)  As long as the file is open (input) EOF is technically wrong. EOF reads beyond line endings.
« Last Edit: June 15, 2026, 02:02:42 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

tetrastes

  • Hero Member
  • *****
  • Posts: 769
I don't bother if it technically wrong or not, but original program can parse multuline input (and piping of multiline files), what EOLN variant cannot do, of course:

Thaddy

  • Hero Member
  • *****
  • Posts: 19498
  • Glad to be alive.
That is what I wrote..... O:-) It is simply how read and readln works that Marcov is right.
« Last Edit: June 15, 2026, 05:43:36 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

 

TinyPortal © 2005-2018