Solved

Either Array or loop sentinel problem??

Posted on 2003-11-05
8
302 Views
Last Modified: 2010-08-05
I believe the problem I am having is the actual reading of the data, although I CAN get it to print out what it reads, but the code is incorrect.  If I change "j" to 4, I get a "Trace/BPT trap" error.  If I change it to 3, it runs fine... but I don't think it's actually breaking the loop via the sentinel.  The reason I come to this conclusion is not just because the error, but because when it does run "correctly," when I try to access the array of records, it is NOT printing out the correct items.   Any help would be greatly appreciated.

Lets say the master file has the following in it:
1111   Jay 10.0 0.0 0.0 0.0
1222 Ariel 20.0 0.0 0.0 0.0
1333  John 30.0 0.0 0.0 0.0
-1

This is part of my program, excess has been deleted for simplicity sake, and because my focus is on the reading part:

PROGRAM payroll(master, output);

VAR master: text;

type emp = RECORD
     num : integer;
     name : ARRAY[1..7] of char;
     sal : real;
     s_YTD : real;
     t_YTD : real;
     f_YTD : real;
     END;
VAR item : ARRAY[1..3] of emp;

PROCEDURE readMasterFile;
  VAR count : integer;
  VAR j, n : integer;
  LABEL 50,100;

  BEGIN
    while NOT EOF (master) DO
     BEGIN
       FOR j:= 1 to 3 DO
         WITH item[j] DO
           BEGIN
            while NOT EOLN (master) DO
             BEGIN
               read(master,item[j].num);
               IF item[j].num = -1 THEN GOTO 100;
               IF NOT item[j].num= -1 THEN GOTO 50;
50:            write(item[j].num);
             FOR count := 1 TO 7 DO
               BEGIN
                 read(master, item[j].name[count]);
                 write(item[j].name[count]);
               END;
             read(master, item[j].sal);
             write(item[j].sal:5:2);  
             read(master, item[j].s_YTD);
             write(item[j].s_YTD:5:2);
             read(master, item[j].t_YTD);
             write(item[j].t_YTD:5:2);
             read(master, item[j].f_YTD);
             write(item[j].f_YTD:5:2);
             END;
            END;
      readln(master);
      writeln();        
      END;  
100:END;

BEGIN
   writeln();
   reset(master);
   readMasterFile;
   writeln();
END.
0
Comment
Question by:GunNam
  • 5
  • 3
8 Comments
 
LVL 100

Accepted Solution

by:
mlmcc earned 50 total points
ID: 9686694
>>If I change "j" to 4, I get a "Trace/BPT trap" error
Problem is the file never reads the EOF character until you try to read the next record after it encoutners the -1.

>>If I change it to 3, it runs fine... but I don't think it's actually breaking the loop via the sentinel.
You are rereading the array.

You either use the for loop or a sentinel loop.  It is very difficult if not impossible to combine the two.  Although this is homework, you have done a lot of good coding.  Your problem is in the looping structures and where you are testing for the last record.

The code is very poorly structured.  You should avoid the use of gotos and use the structures correctly.

Since the EOF is marked with a -1 you shouldn't need to test for EOF.

Initially let's assume the input file is built correctly and has at least one line of data even if it is the -1.


reccount := 0;
Read (master, ID);
While (ID <> -1) DO
  Begin
    reccount := reccount +1;
    item[reccount].num := ID;
    FOR count := 1 TO 7 DO
       BEGIN
           read(master, item[reccount].name[count]);
           write(item[reccount].name[count]);
       END;
     read(master, item[reccount].sal);
     write(item[reccount].sal:5:2);  
     read(master, item[reccount].s_YTD);
     write(item[reccount].s_YTD:5:2);
     read(master, item[reccount].t_YTD);
     write(item[reccount].t_YTD:5:2);
     read(master, item[reccount].f_YTD);
     write(item[reccount].f_YTD:5:2);
     readln(master);
     Read (master, ID);
  End;


Once this is working it can be easily modified to handle potential errors.

mlmcc
0
 

Author Comment

by:GunNam
ID: 9686779
mlmcc, I know the "goto's" shouldn't be used, but this is my first pascal program and that's the only way I could actually get it from looping infinitely.  Thanks for the example you've given, I'm going to fiddle with it now.
0
 

Author Comment

by:GunNam
ID: 9687262
I am getting the Trace/BPT error after I changed my code to the following:  Any ideas?  That error doesn't give any hints as to where the error is so I don't even know where it's going wrong.  I followed through the code, and it looks ok...

PROCEDURE readMasterFile;
   VAR count : integer;
   VAR j, ID : integer;

   BEGIN
     j:=1;
     read(master, ID);
     while (ID <> -1) DO
       BEGIN
         j := j+1;
         item[j].num:=ID;
         write(item[ID].num);
         FOR count := 1 TO 7 DO
           BEGIN
             read(master, item[j].name[count]);
             write(item[j].name[count]);
           END;

         read(master, item[j].sal);
         write(item[j].sal:5:2);
         read(master, item[j].s_YTD);
         write(item[j].s_YTD:5:2);
         read(master, item[j].t_YTD);
         write(item[j].t_YTD:5:2);
         read(master, item[j].f_YTD);
         write(item[j].f_YTD:5:2);
         readln(master);
         read(master, ID);
       END;
   END;
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:GunNam
ID: 9687686
I just realized that I'm not using "WITH" to go through the array of records... can this be done through "WHILE?"

For example,

 BEGIN
     j:=1;
     read(master, ID);
     while (ID <> -1) DO
       WITH item[j] DO
       BEGIN
         j := j+1;
         item[j].num:=ID;
         ...
0
 

Author Comment

by:GunNam
ID: 9687729
Using the "WITH" here is not mandatory because I've written out the whole "address of the record" so to speak...

now I'm lost...
0
 

Author Comment

by:GunNam
ID: 9688210
mlmcc:  I don't know if you were just waiting for me to figure it out... but I did.

I couldn't have done it without your example.  Thank you very much.  Btw, I screwed it up trying to 'write' something after this part of your code:

reccount := reccount +1;
    item[reccount].num := ID;
    write(item[***ID***].num;  (*my incorrect line*)

Just in case you were wondering...
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 9691918
No, I wasn't waiting, I don't receive the emails until I get home.  I don't want to flood my work system and I really shouldn't be doing iot there.  I do a little at lunch then wait til I get home  Tonight i am rather late getting started.

The WITH allows you to drop the record name and use the fields directly.  For instance

      with item[j]
         begin
           .num:=ID;
           write(.num);
         FOR count := 1 TO 7 DO
           BEGIN
             read(master, .name[count]);
             write(.name[count]);
           END;

I think I see your other problem
First j should be initialized to 0 not 1 since you haven't read any good data yet.  The other possibility is you are writeing item[ID] rather than item[j]

     j:=0;                  *********************
     read(master, ID);
     while (ID <> -1) DO
       BEGIN
         j := j+1;
         item[j].num:=ID;
         write(item[j].num);   *****ID should be j ********

mlmcc
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 9691923
Glad I could help

mlmcc
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
default value in Delphi component 2 519
detect keyboard input monitoring 3 393
How to capture screen window of a process 4 407
Can you change icon dynamically in Delphi? 3 693
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Building a successful professional career is a long and difficult journey, especially in case if your decisions are not chosen carefully. For example, if you think that you can get to the desired position without experience and apply for it, your ch…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question