PROGRAM TWENTY-THREE
Write a program which adds up a list of numbers from a file.
Create a sample file to test your program.
{developed from a routine in OH PASCAL, pg 444 }
program PROG23A (input,output,outfile ); {create a file of integers }
var outfile : file of integer;
current, total : integer;
fname : string[15];
begin
total := 0;
writeln('Enter name of file to contain numbers');
readln (fname);
assign( outfile, fname );
rewrite( outfile );
writeln('Enter in integers, a value of 0 stops');
read( current );
while current <> 0 do
begin
write( outfile, current);
read( current )
end;
close( outfile )
end.
{developed from a routine in OH PASCAL, pg 444 }
program PROG23 (input,output,infile ); {sum of integers in a file}
var infile : file of integer;
current, total : integer;
fname : string[15];
begin
total := 0;
writeln('Enter name of file containing numbers');
readln (fname);
assign( infile, fname );
reset( infile );
while not eof( infile ) do
begin
read( infile, current );
total := total + current
end;
writeln('The sum of all numbers is ', total)
end.
Copyright B Brown/P Henry/CIT, 1988-1997. All rights reserved.