Solved

Either Array or loop sentinel problem??

Posted on 2003-11-05
8
301 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Troubleshooting common task sequence error codes
When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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