William Brimer
William Brimer

Reputation: 13

Trying to get Pascal to read from file then add all of the even numbers having issues.

OK so I am trying to get Pascal to read a set of integers from a input file then take the even numbers and add them together with in the range of 1-50. Not working for me. Here is what I have: List of input file numbers 1 2 3 4 5 6 7 8 9 11 12 64 13 14 15 16 71 33 34 35 36 41 44 46 82 512 49 50 And my pascal program:

program even(input,output,indata); 
uses crt;
Var
  indata:text;
  num, even:integer;
begin
  clrscr;
  assign(indata, 'j:\num.txt');
  reset(indata)0;
  read(indata, num);
  while num>50 do
  begin
         read(indata, num);
         if num mod 2=0 then
         even:=even+num;
  end;
  writeln('Even sum is', even);
  readln( );
end. 

Any help would be great! When I run it I get text as "Even sum is 0".

Upvotes: 1

Views: 1706

Answers (3)

John Saltwell
John Saltwell

Reputation: 43

The previous posters have given good advice but I would add at the end (before end.)

CloseFile(infile) or Close(infile)

(depending on your version of Pascal). This will probably not affect the program output but is a good habit to get into for the future - if you're writing to (rather than reading from) a text file and don't close it, you may find some text is missing, or the file becomes unreadable.

Upvotes: 0

Ken White
Ken White

Reputation: 125708

You need to first fix your code so it will even compile. The line that calls reset has the 0 after the closing ), which is invalid syntax.

You need to first initialize your even variable, so that it contains a valid starting point.

Next, you need to fix the logic in your while loop. It's backwards. :-) You need to test for num < 50 if you want to stop at the end - the test you have now for num > 50 means that the loop will never execute, because the first value (1) ends the while loop.

This works in a plain console application in Delphi.

program Project2;

uses
  SysUtils;

var
  InData: Text;
  num, even: Integer;

begin
  AssignFile(InData, 'D:\TempFiles\numbers.txt');
  reset(indata);
  read(indata, num);
  even := 0;
  while num < 50 do
  begin
    read(indata, num);
    if num mod 2 = 0 then
       even := even + num;
  end;
  writeln('Even sum is ', even);
  readln;
end.

It produces the output (which is correct according to the way your code is written, because it reads the value 64 inside the while num loop and therefore executes 1 time more than it should, and 64 is even so it gets added to even - I'll leave that for you to figure out). :-)

Even sum is 96.

Upvotes: 1

Mark Stevens
Mark Stevens

Reputation: 2366

Haven't done Pascal in a few decades, but if I recall:

You're saying:

while num > 50 do

and your first number is 1, so it just skips the loop and prints the initial value of 'even', which is 0.

You may have meant:

while num < 50 do

but even that will just exit at the first value 50 or greater.

If you mean to read all the numbers, but filter out those greater than 50, I think it's more like:

program even(input,output,indata); 
uses crt;
Var
  indata:text;
  num, even:integer;
begin
  clrscr;
  assign(indata, 'j:\num.txt');
  reset(indata)0;  

  (* read all of the numbers *)
  while not eof(indata) do
  begin
         read(indata, num);

         (* skip those greater than 50 *)
         if num <= 50
         begin
             if num mod 2=0 then
             even:=even+num;
         end;
  end;
  writeln('Even sum is', even);
  readln( );
end. 

Upvotes: 0

Related Questions