Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Either Array or loop sentinel problem??

Posted on 2003-11-05
8
Medium Priority
?
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Check out what's been happening in the Experts Exchange community.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

688 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