Reputation: 115
I'm having trouble reading more than one line from a data file in Pascal. It gives me an "Invalid numeric format" run-time error when I try to read in more than one line (I tested just the first line separately and it works fine). This makes me think that it has something to do with the carriage return at the end of a line.
Here is the code that should read in all of the lines from my .DAT file:
program commission;
var
moreRec:Boolean;
FileOut:Text;
FileIn:Text;
DRONE_ID:String[9];
DRONE_NAME:String[18];
SALES:Real;
COMM:Real;
procedure header;
begin
writeln(FileOut, Space(16),'SALES COMMISSION REPORT');
writeln(FileOut);
writeln(FileOut,' SSN',Space(10),'SALESPERSON',Space(9),'SALES COMMISSION');
writeln(FileOut);
end;
procedure readRec;
begin
if EOF(FileIn) THEN
moreRec:=false
else
read(FileIn, DRONE_ID);
read(FileIn, DRONE_NAME);
read(FileIn, SALES);
COMM:=SALES*0.03;
end; {readRec}
procedure initial;
begin
moreRec:=true;
Assign(FileIn, 'PRG2-150.DAT ');
Reset(FileIn);
Assign(FileOut,'output.txt');
Rewrite(FileOut);
readRec
end; {initial}
procedure process;
begin
write(FileOut, DRONE_ID);
write(FileOut, Space(2));
write(FileOut, DRONE_NAME);
write(FileOut, Space(5));
write(FileOut, SALES:9:2);
write(FileOut, Space(3));
writeln(FileOut, COMM:8:2);
readRec
end; {process}
procedure wrapup;
begin
Close(FileOut);
Close(FileIn);
end; {wrapup}
begin
initial;
header;
while moreRec = true do
process;
wrapup;
end.
And here is the .DAT file that I am reading from:
998874673Joe Smith 27.65
849773298Sue Williams 35.90
445861253Al Oop 54.90
584988754Diane Mindykowski 25.96
758423652Alicen Morse 53.35
485236845Burton Schuring 58.52
586974512Linda Gillam 69.35
I'm new to Pascal but I'd love to learn why my program won't read in more than one line.
Thanks
Upvotes: 0
Views: 803
Reputation: 885
Pascal wants the data fields in text files to be white-space delimited. The problem is that you have no space between the Drone_Id and Drone_Name fields.
998874673Joe Smith 27.65
_________^__ Insert space here.
You also should use a readln for the last field on the line (Sales).
EDIT: Sorry the space isn't needed there (I was thinking that the first field was numeric). But do make sure you use readln on the last field of the line.
Upvotes: 0
Reputation: 28839
I think you'll need a
readln(FileIn);
towards the end of readRec to skip past the CR/LF delimiter to the next line.
Upvotes: 2