So basically I was supposed to take my prior lab which read a text file that had the hours and wage:
15 10
50 15
30 10

I needed to add the name to my text file
Doe Jane 15 10
Doe John 50 15
Doe Joe 30 10

My problem comes when I wrote the following code
Readln(Infile, last, first, hours, wage);

Now I understand it has to do with the fact that I start with string variables so it confused the program by then having Real variables following.

So I tried to seperate the two
Readln(Infile, last, first);
Readln(Infile, hours, wage);

But then it will print the name but do an error 106 for the hours and wage.

I am confused and unfortunately my teacher is not very helpful (online class and she doesn't respond to emails). If someone could please explain what I should do or read that can explain this I would greatly appreciate it. The textbook the teacher assigned for the class is for pseudocode and its useless when I am trying to do pascal.

Thanks in advance for your help.

Program lab4;
Var
    hours, hoursReg, hoursOver, wage, payReg, payOver, payGross, aveGross,
    minGross, maxGross, totalGross: Real;

    Count:Integer;

    last, first: String;

    Outfile,
    Infile:Text;

    LineCt:Integer;
    firstTime, firstRecord:Boolean;

Const
     timeAndHalf = 1.5;
     PAGESIZE = 5;

Begin
Assign(Infile, 'C:\Users\Desktop\lab6.txt');
Reset(Infile);
Assign(Outfile, 'C:\Users\Desktop\out.txt');
Rewrite(Outfile);

LineCt := PAGESIZE + 1;
firstTime := True;
firstRecord := True;
count := 0;
totalGross := 0;

While Not EOF(Infile) Do
 Begin
  If LineCt >= PAGESIZE
   Then
    Begin
     IF FirstTime
      Then
       FirstTime := False
      Else
        Write(Outfile, chr(12));
     Writeln(Outfile, 'Name Regular  Overtime  Pay    Regular   Overtime  Gross');
     Writeln(Outfile, '     Hours    Hours     Rate   Pay       Pay       Pay');
     Writeln(Outfile);
     LineCt := 3;
    End;
  Read(Infile, last, first, hours, wage);*********MY ERROR IS RIGHT HERE********** 

  If hours > 40 Then
    Begin
    hoursOver := hours - 40;
    payOver := hoursOver * (wage * timeAndHalf);
    End;

hoursReg := hours - hoursOver;
payReg := hoursReg * wage;
payGross := payReg + payOver;


  If firstRecord
     Then
         Begin
          minGross := payGross;
          maxGross := payGross;
          firstRecord := False;
         End
  Else
      Begin
           If minGross > payGross
            Then
             minGross := payGross;
            If maxGross < payGross
             Then
              maxGross := payGross
      End;

totalGross := totalGross + payGross;
count := count + 1;

Writeln(Outfile, last, first, hoursReg:0:2, hoursOver:9:2, wage:10:2, payReg:8:2, payOver:10:2,
payGross:10:2);
LineCt := +1;

End;

aveGross := totalGross / count;

Writeln(Outfile, '');
Writeln(Outfile, 'Total Gross Pay: ', totalGross:12:2);
Writeln(Outfile, 'Number of Employees: ',  Count:5);
Writeln(Outfile, 'Average Gross Pay: ', aveGross:10:2);
Writeln(Outfile, 'Maximum Gross Pay: ', maxGross:10:2);
Writeln(Outfile, 'Minimum Gross Pay: ', minGross:10:2);

Close(Outfile);

Readln;

end.



hours, wage);

  If hours > 40 Then
    Begin
    hoursOver := hours - 40;
    payOver := hoursOver * (wage * timeAndHalf);
    End;

hoursReg := hours - hoursOver;
payReg := hoursReg * wage;
payGross := payReg + payOver;


  If firstRecord
     Then
         Begin
          minGross := payGross;
          maxGross := payGross;
          firstRecord := False;
         End
  Else
      Begin
           If minGross > payGross
            Then
             minGross := payGross;
            If maxGross < payGross
             Then
              maxGross := payGross
      End;

totalGross := totalGross + payGross;
count := count + 1;

Writeln(Outfile, hoursReg:0:2, hoursOver:9:2, wage:10:2, payReg:8:2, payOver:10:2,
payGross:10:2);
LineCt := +1;

End;

aveGross := totalGross / count;

Writeln(Outfile, '');
Writeln(Outfile, 'Total Gross Pay: ', totalGross:12:2);
Writeln(Outfile, 'Number of Employees: ',  Count:5);
Writeln(Outfile, 'Average Gross Pay: ', aveGross:10:2);
Writeln(Outfile, 'Maximum Gross Pay: ', maxGross:10:2);
Writeln(Outfile, 'Minimum Gross Pay: ', minGross:10:2);

Close(Outfile);

Readln;

end.

Recommended Answers

All 2 Replies

The first line of your input file is:
Doe Jane 15 10
and you are trying to read it like this:
Read(Infile, last, first, hours, wage);

So you are assuming that the READ command will automatically split the line at the spaces to produce the 4 variables, and convert the digit characters to numeric values to match your numeric variables. Unfortunately it isn't so easy. I suggest that instead of READ you use READLN to read the whole line into a single string. Then locate the spaces (perhaps by walking through the string chr by chr or by using pos) and split the line into 4 strings, one for each field. Hint: COPY can be used to extract the substrings you need. Then convert the strings of digits into numeric values. Exactly how you do that will depend on your compiler and also on what commands you are expected to know about.

Also check out your code from line 99 onwards. I think this is just text appearing after the end of the program and the compiler will ignore it. If you did try to compile it line 103 would give an error.

Thanks for the help.

Changed to readln.. And realized my major error was the way I had it in the txt file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.