RPGLE Increment Counter into Data Structure

Viewed 194

I'm trying to implement a counter in my program that will give a total at the end of the records. Currently I have a counter within a procedure called TotalCalls += 1; that counts the calls which is working fine, and I have a Totals field in my print file that I need to move that into. The problem is that I have a datastructure Dcl-ds PrinterTotalDS likeRec(PrinterFile.Totals:*output); that I need to move the counter data into and then write that to the print file. I'm currently using this as the write: Write PrinterFile.Totals PrinterTotalDS; but nothing shows up. When I debug the program the counter works but nothing is moved to the write for totals. Here is the code in it's entirety: Any help is greatly appreciated!

/Copy IMPWR126/QRPGLESRC,S071303CPY

Dcl-F PrinterFile PRINTER Qualified Alias ExtFile(*extdesc) OfLind(EndOfPage) USROPN
                     ExtDesc('S071303P');

Dcl-F S071303LF Keyed;

Dcl-s EndofPage ind inz(*on);
Dcl-s DateConv Date;
Dcl-s TimeConv Time;
Dcl-s Tempstring Char(25);
Dcl-s DayofWk Char(15);
Dcl-s InputDate Zoned(8:0);
Dcl-s TotalCalls Zoned(5:0);

Dcl-ds PrinterHeadDS likeRec(PrinterFile.Header:*output);
Dcl-ds OutputDS likeRec(PrinterFile.Detail:*output);
Dcl-ds InputDS likeRec(CALLREC);

Dcl-Proc Driver;
  Dcl-Pi *N;
      CALDAT Zoned(8:0);
  END-PI;

  Dcl-ds PrinterTotalDS likeRec(PrinterFile.Totals:*output);

  Open PrinterFile;

  Clear InputDS;
  Read S071303LF InputDS;
  Clear OutputDS;

  DOW not %eof(S071303LF);
    TotalCalls += 1;
    MoveDS();
    Eval-Corr OutputDS = InputDS;
    IF EndofPage;
      EndOfPage = *off;
      Write PrinterFile.Header PrinterHeadDS;
    ENDIF;
    Write PrinterFile.Detail OutputDS;
    Read S071303LF InputDS;
    Clear OutputDS;
  Enddo;
  TotalCalls = %dec('0' + %trim(PrinterTotalDS):5:0);
  Write PrinterFile.Totals PrinterTotalDS;
  close S071303LF;
  close PrinterFile;
  return;
END-PROC Driver;

Dcl-Proc MoveDS;
    OutputDS.DateConv = CONVDATE(InputDS.CALDAT);
    OutputDS.TimeConv = CONVTIME(InputDS.CALTIM);
    TempString = InputDS.CFNAME;
    OutputDS.FirstName = PROPCASE(TempString);
    TempString = InputDS.CLNAME;
    OutputDS.LastName = PROPCASE(TempString);
    OutputDS.DayofWk = GetDayofWeek(InputDS.CalDat);
End-Proc;
2 Answers

Your problem is on this line right here:

  TotalCalls = %dec('0' + %trim(PrinterTotalDS):5:0);

You are overwriting TotalCalls with some value from the PrinterTotalDS. I'm not even remotely sure why you are doing a trim on a data structure with a leading 0 and casting it to a decimal but that's not really relevant to the actual issue.

Instead, I think you want something like this:

PrinterTotalDS.MyOutputField = TotalCalls;
Write PrinterFile.Totals PrinterTotalDS;

I think you could just use the counter in PrinterTotalDS instead of having a separate TotalCalls field.

But make sure you add the INZ keyword to the definition of PrinterTotalDS.

(It's a good idea to initialize all data structures. By the somewhat bizarre default, data structures get initialized to blanks, which means most subfields get initialized with invalid values.)

Related