Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Either Array or loop sentinel problem??

Posted on 2003-11-05
8
Medium Priority
?
307 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 101

Accepted Solution

by:
mlmcc earned 200 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
Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

 

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 101

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 101

Expert Comment

by:mlmcc
ID: 9691923
Glad I could help

mlmcc
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to fix a SonicWall Gateway Anti-Virus firewall blocking automatic updates to apps like Windows, Adobe, Symantec, etc.
Following on from our article on "The Murky World of Consent and opt in", we thought we would issue some helpful guidance, not only on consent itself but knowing what information you are capturing, what you are doing with this data and how you can p…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Screencast - Getting to Know the Pipeline

877 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