3

OK so I'm doing assignment but then I found that I was asked to add page numbers and change pages for each 4 records. Since it's an online course and I don't think there is anything about page numbers in lecture videos. So the main problems are

  1. To add a heading that contains date and page number,
  2. Print 4 records per page, which means page needs to be changed after printing 4 records.

I really have no idea how to do this.

Here is the code I have finished:

   ENVIRONMENT DIVISION.
   FILE-CONTROL.   SELECT STOCK-IN ASSIGN TO 'F:/CS201S13/PROJECT2.TXT'
                   ORGANIZATION IS LINE SEQUENTIAL.
                   SELECT STOCK-OUT ASSIGN TO 'F:/CS201S13/PROJECT2OUTPUT.TXT'
                   ORGANIZATION IS LINE SEQUENTIAL.
   DATA DIVISION.
   FILE SECTION.
   FD STOCK-IN.
   01 STOCK-RECORD.
       05 ST-TRANSACTION-INFORMATION.
           10 ST-TRANSACTION-SHARES    PIC 9(3).
           10 ST-TRANSACTION-STOCK     PIC X(14).
       05 ST-PURCHASE-INFORMATION.
           10 ST-PURCHASE-PRICE        PIC 9(5)V99.
           10 ST-PURCHASE-DATE.
               15 ST-PURCHASE-YEAR     PIC 99.
               15 ST-PURCHASE-MONTH    PIC 99.
               15 ST-PURCHASE-DAY      PIC 99.
       05 ST-SALE-INFORMATION.
           10 ST-SALE-PRICE            PIC 9(5)V99.
           10 ST-SALE-DATE.
               15 ST-SALE-YEAR         PIC 99.
               15 ST-SALE-MONTH        PIC 99.
               15 ST-SALE-DAY          PIC 99.
   FD STOCK-OUT. 
   01 STOCK-RECORD-OUT.
       05 ST-TRANSACTION-INFORMATION-OUT.
           10 ST-TRANSACTION-SHARES-OUT PIC 9(3).
           10 ST-TRANSACTION-STOCK-OUT  PIC X(14).
       05 TOTAL-PURCHASE               PIC 9(8)V99.
       05                              PIC X(4).
       05 TOTAL-SALE                   PIC 9(8)V99.
       05                              PIC X(4).
       05 TOTAL-PROFIT                 PIC 9(8)V99.
       05                              PIC X(4).
       05 ST-PURCHASE-DATE-OUT.
           10 ST-PURCHASE-YEAR-OUT     PIC 99.
           10                          PIC X VALUE '/'.
           10 ST-PURCHASE-MONTH-OUT    PIC 99.
           10                          PIC X VALUE '/'.
           10 ST-PURCHASE-DAY-OUT      PIC 99.
       05                              PIC X(4).
       05 ST-SALE-DATE-OUT.
           10 ST-SALE-YEAR-OUT         PIC 99.
           10                          PIC X VALUE '/'.
           10 ST-SALE-MONTH-OUT        PIC 99.
           10                          PIC X VALUE '/'.
           10 ST-SALE-DAY-OUT          PIC 99.
       05                              PIC X(4).
       05 RECORD-OUT                   PIC 9 VALUE 0.
       05 PAGE-OUT                     PIC 9.
   WORKING-STORAGE SECTION.
   01 ARE-THERE-MORE-RECORDS PIC XXX VALUE 'YES'.
   01 IS-THIS-PAGE-FULL      PIC XXX VALUE 'NO '.
   PROCEDURE DIVISION.
   100-MAIN-PROCESS.
   OPEN INPUT STOCK-IN
        OUTPUT STOCK-OUT
   MOVE ST-TRANSACTION-INFORMATION TO ST-TRANSACTION-INFORMATION-OUT
   PERFORM UNTIL ARE-THERE-MORE-RECORDS = 'NO '
       READ STOCK-IN
           AT END
               MOVE 'NO ' TO ARE-THERE-MORE-RECORDS
           NOT AT END
               PERFORM 200-PROCEDURE-RTN
               ADD 1 TO RECORD-OUT
       END-READ
   END-PERFORM
   CLOSE STOCK-IN
         STOCK-OUT
   STOP RUN.
   200-PROCEDURE-RTN.
   IF RECORD-OUT = 4
       MOVE 'YES' TO IS-THIS-PAGE-FULL
       MOVE 0 TO RECORD-OUT
       MOVE 'NO ' TO IS-THIS-PAGE-FULL
       ADD 1 TO PAGE-OUT
   END-IF
   MULTIPLY ST-PURCHASE-PRICE BY ST-TRANSACTION-SHARES GIVING TOTAL-PURCHASE
   MULTIPLY ST-SALE-PRICE BY ST-TRANSACTION-SHARES GIVING TOTAL-SALE
   SUBTRACT TOTAL-PURCHASE FROM TOTAL-SALE GIVING TOTAL-PROFIT
   WRITE STOCK-RECORD-OUT.
2
  • +1 for learning COBOL
    – SnakeDoc
    Apr 4, 2013 at 1:50
  • I have zero understanding of COBOL, however you will likely need a counter variable to store what page number you are on, and to increment when needed.
    – SnakeDoc
    Apr 4, 2013 at 1:56

2 Answers 2

5

You are both close, and far away.

"Close" because you need a little bit of code in between setting IS-THIS-PAGE-FULL to YES and NO.

"Far away" as you have quite a lot to do rather than just "patch up" what you have.

Is the program writing an output file (STOCK-OUT) and a report, or is STOCK-OUT the report? If it is a report, change the names so that it is clear that it is a report, not an output file.

Don't worry if this seems a lot. You should be learning how to Program in Cobol, as well as learning Cobol. Doesn't happen overnight.

In no particular order:

  • Include FILE-STATUS checking for all IO operations on all files, always. At the moment, if your input fails to open and the system does not fail the program (even if yours does, you are presumably learning Cobol to be able to work with any system, not just the one you have) then no records will be read, your "end of file test" will never be YES and you'll have a BFL (Big Fat Loop). With the FILE-STATUS checking, produce useful messages, including key/reference/record number as appropriate for failed READ or WRITE.

You may feel that this is a lot of work. However, put together some "template" files with all the stuff in, and then paste (or even COPY) those into your program each time.

  • You have VALUE clause in the FD. These will not do what you think.

  • You have single digit for your page count, which is unlikely to have general application.

  • Why use YES and NO as literals? Look at the SET verb, in relation to "condition names", use 88's for tests and "flags/switches".

  • You have "MOVE ST-TRANSACTION-INFORMATION" after the input is opened but before a record is read, and only have one reference to it in the program. This is not going to work.

  • For reading files, have a look at the "priming read" approach.

    read input loop until end-of-file (88 on file-status) process data read input end-loop

This avoids the AT END/NOT AT END, allows processing of headers (if present) and "empty files" without clogging-up the main logic. The code "expands" with headers/trailers (including the correct number of them), sequence-checking of keys, etc, but you only need to code it once then "template" it.

  • According to your VALUE clauses in your FD, you expect RECORD-OUT to be zero, so the test for 4 will actually get you five on the first page, and four thereafter.

  • You always assume there will be a "profit" (a positive amount), which is not realistic, yet you don't allow a signed value for the "profit".

Now, for the report.

For your report FD, just make it a simple thing, length of your print line.

In WORKING-STORAGE, define data for the headings and titles that you need. Define data for a print line. Since you're in the WORKING-STORAGE, put VALUEs for everything which will not have data MOVEd to it in the PROCEDURE DIVISION.

When you have written four items (or when your program tells you this) and you have a fifth, write the headings and titles, remembering to update the page number.

I say "or when your program tells you this" because you can set your original value of "records written" to 4. Comment it, so that it is clear that it is what you want, and why you want it. The reason is, you don't have to then deal with "first time" headings and othe things. For first time, or on a "contol break" (I guess you'll get to those soon) set the " done on a page already" to the maximum for a page, and the headings will pop out when you want.

Format the print line. PERFORM a para to print it (which is where the "page full" test will be).

Note: You can use VALUEs for your "/"s in the dates, or you can use the "/" editing character in the PICture, like this:

05  an-input-date PIC X(8) (can be other definitions).
...
05  date-to-print PIC X(4)/XX/XX.
...
MOVE an-input-date TO date-to-print

I like to see that you are using "minimal full-stops/periods". You can go a little further.

MOVE an-input-date TO date-to-print
.

Then you get your final full-stop/period in a paragraph, without having it "attached" to any particular line of code, which makes "tossing code around" easier, as you don't have to think "do I need/not need that full-stop/period there".

You could also look through some of the Cobol questions here, and get a handle on some general tips and advice.

1
  • Thanks for your advice, they're pretty useful and I'm revising my assignment now. Thank you!
    – Luyao Tian
    Apr 7, 2013 at 21:39
2

This may or may not help, if LINAGE is not supported you'll have to do some explicit counting.

  *****************************************************************
  * Example of LINAGE File Descriptor
  * Author: Brian Tiffin
  * Date:   10-July-2008
  * Tectonics: $ cobc -x linage.cob
  *            $ ./linage <filename ["linage.cob"]>
  *            $ cat -n mini-report
  *****************************************************************
   IDENTIFICATION DIVISION.
   PROGRAM-ID. linage-demo.

   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
      select optional data-file assign to file-name
          organization is line sequential
          file status is data-file-status.
      select mini-report assign to "mini-report".

   DATA DIVISION.
   FILE SECTION.
   FD  data-file.
   01  data-record.
      88 endofdata        value high-values.
      02 data-line        pic x(80).
   FD  mini-report
      linage is 16 lines
          with footing at 15
          lines at top 2
          lines at bottom 2.
   01  report-line         pic x(80).

   WORKING-STORAGE SECTION.
   01  command-arguments   pic x(1024).
   01  file-name           pic x(160).
   01  data-file-status    pic 99.
   01  lc                  pic 99.
   01  report-line-blank.
      02 filler           pic x(18) value all "*".
      02 filler           pic x(05) value spaces.
      02 filler           pic x(34)
          VALUE "THIS PAGE INTENTIONALLY LEFT BLANK".
      02 filler           pic x(05) value spaces.
      02 filler           pic x(18) value all "*".
   01  report-line-data.
      02 body-tag         pic 9(6).
      02 line-3           pic x(74).
   01  report-line-header.
      02 filler           pic x(6) VALUE "PAGE: ".
      02 page-no          pic 9999.
      02 filler           pic x(24).
      02 filler           pic x(5) VALUE " LC: ".
      02 header-tag       pic 9(6).
      02 filler           pic x(23).
      02 filler           pic x(6) VALUE "DATE: ".
      02 page-date        pic x(6).

   01  page-count          pic 9999.

   PROCEDURE DIVISION.

   accept command-arguments from command-line end-accept.
   string
      command-arguments delimited by space
      into file-name
   end-string.
   if file-name equal spaces
      move "linage.cob" to file-name
   end-if.

   open input data-file.
   read data-file
      at end
          display
               "File: " function trim(file-name)
               " open error or empty"
          end-display
          go to early-exit
   end-read.

   open output mini-report.

   write report-line
      from report-line-blank
   end-write.

   move 1 to page-count.
   accept page-date from date end-accept.
   move page-count to page-no.
   write report-line
      from report-line-header
      after advancing page
   end-write.

   perform readwrite-loop until endofdata.

   display
      "Normal termination, file name: "
      function trim(file-name)
      " ending status: "
      data-file-status
   end-display.
   close mini-report.

  * Goto considered harmful?  Bah!  :)
   early-exit.
   close data-file.
   exit program.
   stop run.

  ****************************************************************
   readwrite-loop.
   move data-record to report-line-data
   move linage-counter to body-tag
   write report-line from report-line-data
      end-of-page
          add 1 to page-count end-add
          move page-count to page-no
          move linage-counter to header-tag
          write report-line from report-line-header
              after advancing page
          end-write
   end-write
   read data-file
      at end set endofdata to true
   end-read
   .

  *****************************************************************
  * Commentary
  * LINAGE is set at a 20 line logical page
  *  16 body lines
  *   2 top lines
  *   A footer line at 15 (inside the body count)
  *   2 bottom lines
  * Build with:
  * $ cobc -x -Wall -Wtruncate linage.cob
  * Evaluate with:
  * $ ./linage
  * This will read in linage.cob and produce a useless mini-report
  * $ cat -n mini-report
  *****************************************************************
   END PROGRAM linage-demo.
3
  • It seems more efficient but there are too many things that I don't know since I just start learning COBOL. But I can use it as reference. Thanks!
    – Luyao Tian
    Apr 7, 2013 at 20:07
  • Luyao Tian, wait till Brian and friends get the REPORT WRITER feature going in OpenCobol. It will make many reporting requirements much simpler to code and maintain, but you'll need to know how to code reports the "hard way" as well, for compilers without the feature available. Apr 9, 2013 at 23:10
  • Luyao, that code is in the OpenCOBOL FAQ at opencobol.add1tocobol.com/#linage Apr 19, 2013 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.