Rahman
Rahman

Reputation: 11

How to avoid the program to read past the end of input file?

This Pascal code keep attempting to read past the end of input file. Why this could be happened? How to fix this?

program wwhilencounter;
var 
  bil,jumlah : integer;
begin
  jumlah := 0;
  while not eof(input) do
  begin
    readln(bil);
    jumlah:=jumlah+bil;
  end;
  writeln(jumlah);
  readln 
end.

Upvotes: 1

Views: 1088

Answers (1)

JRI
JRI

Reputation: 1932

Your while loop reads until the end of the input, but then you have another readln just before the end of the program. You've already run out of input by this point.

Upvotes: 1

Related Questions