Solved

Either Array or loop sentinel problem??

Posted on 2003-11-05
8
298 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:GunNam
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:GunNam
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Glad I could help

mlmcc
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Delphi - What Does "F" Symbolize? 2 470
Delphi XE 5 - windows 3 855
Regular Expression to Split a String 7 102
code issue 8 84
Marketing can be an uncomfortable undertaking, especially if your material is technology based. Luckily, we’ve compiled some simple and (relatively) painless tips to put an end to your trepidation and start your path to success.
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now