Hi guys,
I am doing an exercise we got in school and I am stucked at the point when I think everything is correct, but obviously is not :-).

Target: Make a very simple pascal program, which opens a text file with three variables(name, sex, salary) and returns the highest women salary.

I have the following:

Program salaries;
uses crt;
var F:text;max,salary:word;name:string[50];sex:0..1;

begin

 max:=0;
 assign(F,'C:\FPC\2.4.0\bin\i386-win32\salary.txt');reset(F);

  repeat
   readln(F,name,sex,salary);
   if (sex=1) and (salary>max) then max:=salary;
  until eof(F);

 clrscr;close(F);
 writeln;
 writeln('The highest ladies salary is:  ',max);
 writeln('Press ENTER for exit.');
 readln;

end.

and text file salary.txt has following content:
Peter 0 3500
Helena 1 2000
John 0 0
Natalia 1 7000
Frank 0 7100


When trying to compile this in FreePascal, there are no errors, but once I run the program, I get exitcode 106. Could you please look at it and advise what should I change?

And another question, how to write it in order to see all items on the row? I mean, if the above written example would work, we would get just number of highest salary(7000), but what to do, if I want also the name of the person with the highest salary?

Thanks a lot for your help,
Pavel.

Edited 6 Years Ago by pavel.pavel: n/a

Dear Povel
your code have abit mistakes that I correct them
but the main reson for Error 106 is not becouse of your Code ; it is becouse of Data format in your input text file named salary.txt
you define variable Name as String[50] in your program and try to run this statement "readln(F,Name,Sex,Salary);"
name is 50 character but in text file you dont consider the length of Name and type names as variable length.
I correct that and write a new programe as below;

Program Salaries;
Uses CRT;
Var F:Text;
    Salary,max:integer;
    Name:String[20];
    MaxName:String;
    Sex:0..1;
Begin
     Max:=0;
     Assign(F,'E:\Exam\Salary.txt');
     Reset(F);
     While not Eof(F) Do
     Begin
          ReadLn(F,Name,Sex,Salary);
          If (Sex=1) and (Salary>Max) then
          Begin
             Max:=Salary;
             MaxName:=Name;
          End
     End;
     CLRSCR;
     Close(F);
     Writeln;
     WriteLn('The highest ladis salary is for Mrs. ',MaxName,'By Value:',Max);
     Writeln;
     Writeln;
     Writeln;
     Writeln;
     WriteLn('Press Enter To Exit');
     ReadLn
End.

the salary.txt file mast be as same as below

Dear Povel
your code have abit mistakes that I correct them
but the main reson for Error 106 is not becouse of your Code ; it is becouse of Data format in your input text file named salary.txt
you define variable Name as String[50] in your program and try to run this statement "readln(F,Name,Sex,Salary);"
name is 50 character but in text file you dont consider the length of Name and type names as variable length.
I correct that and write a new programe as below;

Program Salaries;
Uses CRT;
Var F:Text;
    Salary,max:integer;
    Name:String[20];
    MaxName:String;
    Sex:0..1;
Begin
     Max:=0;
     Assign(F,'E:\Exam\Salary.txt');
     Reset(F);
     While not Eof(F) Do
     Begin
          ReadLn(F,Name,Sex,Salary);
          If (Sex=1) and (Salary>Max) then
          Begin
             Max:=Salary;
             MaxName:=Name;
          End
     End;
     CLRSCR;
     Close(F);
     Writeln;
     WriteLn('The highest ladis salary is for Mrs. ',MaxName,'By Value:',Max);
     Writeln;
     Writeln;
     Writeln;
     Writeln;
     WriteLn('Press Enter To Exit');
     ReadLn
End.

the salary.txt file mast be as same as attached file
consider that I define Name as String[20];

kindly regards
fayyaz

Attachments
Peter               0 3500
Helena              1 2000
John                0 0
Natalia             1 7000
Frank               0 7100

I made a solution too

Program salaries;
uses crt;
var F:text;
    max,salary:word;
    name,temp_name:string[50];(*if it contains 50 chars then we have to reserve this place in the file*)
    sex,temp_sex:0..1;
    line:string;   (*one line of the txt file*)


begin
     clrscr;
     max:=0;
     assign(F,'salary.txt');
     rewrite(f);
     (*here is the reserved place,'cause we have to know where is the end of the 'name' var !*)
     write(f,'Peter':50);
     (*and here we have to put a space too,it is important!*)
     write(f,0,' ');
     (*when we finished with one line
     then write a CarriageReturn(#13) and a LineFeed(#10) sign too (end of line sign!)
     do not worry,it is made by the writeln procedure...*)
     writeln(f,3500);
     (*here do as above!*)
     write(f,'Helena':50);
     write(f,1,' ');
     writeln(f,2000);
     write(f,'John':50);
     write(f,0,' ');
     writeln(f,0);
     write(f,'Natalia':50);
     write(f,1,' ');
     writeln(f,7000);
     write(f,'Frank':50);
     write(f,0,' ');
     write(f,7100);
     close(f);

     reset(f);    (*open the file for reading*)

     repeat
           read(f,name);
           read(f,sex);
           read(f,salary);
           readln(f,line);
           if (sex=1) and (salary>max) then begin
              max:=salary;
              temp_name:=name;
              temp_sex:=sex;
           end;
     until eof(F);
     close(F);
     writeln;
     writeln('Name: ',temp_name,' Sex: ',temp_sex);
     writeln('The highest ladies salary is:  ',max); (*here the result is: 7000*)

     writeln('Press ENTER for exit.');
     readln;

end.

(*created by FlamingClaw 2010.03.09.Hungary*)
This article has been dead for over six months. Start a new discussion instead.