Final Total Lines in COBOL Please be sure you can see the speaker notes - they contain additional information! Final Total Processing Final Total Processing To be able to produce final totals at the bottom of the report, the following coding must be added to your program: First: accumulators must be defined be defined and setup in the Working-Storage Section Second: the final total line or lines must be defined and setup in the Working-Storage Section Third: in the processing routine in your program, the accumulators must be added to Fourth: there must be a code in the terminate part of your program that moves the contents of the accumulators to the final total line and prints out the final totals. This can either be done in the C100-TERMINATE paragraph or a separate paragraph controlled by C-100-TERMINATE (or whatever name is used to define the wrapup or terminate paragraph) WORKING-STORAGE Accumulators are set up in WORKING-STORAGE This shows the three accumulators that will be used to gather the totals as the records are processed. WORKING-STORAGE SECTION. 01 INDICATORZ. 05 EOF-IND PIC XXX VALUE SPACES. 01 ACCUMULATORS. 05 TOTAL-RECORDS-ACC PIC 999 VALUE 0. 05 TOTAL-ON-HAND-ACC PIC 9(6) VALUE 0. 05 TOTAL-ON-ORDER-ACC PIC 9(6) VALUE 0. 01 TOTAL-LINE. Accumulators need to have an 05 FILLER PIC X(16) initial value of 0 so that we start VALUE " TOTAL RECORDS: ". adding to nothing. 05 TOTAL-RECORDS-ACC-TL PIC ZZ9. 05 FILLER PIC X(7) VALUE SPACES. 05 TOTAL-ON-HAND-ACC-TL PIC ZZZ,ZZ9. 05 FILLER PIC XX VALUE SPACES. 05 TOTAL-ON-ORDER-ACC-TL PIC ZZZ,ZZ9. 05 FILLER PIC X(38) VALUE SPACES. When EOF has been reached, the contents of the three accumulators will be moved to the fields on the total line and then the total line will be written. COBOL RULE: VALUEs are not given to edited fields in WORKING-STORAGE. 01 level of FD - PRINTZ Detail print line - in FILE SECTION 01 PRINTZ. 05 05 05 05 05 05 05 05 05 05 05 05 05 05 05 05 05 FILLER ITEMNO-PR FILLER ITEMNAME-PR FILLER ON-HAND-PR FILLER ON-ORDER-PR FILLER REORD-PT-PR FILLER COST-PR FILLER PRICE-PR FILLER VENDORID-PR FILLER ITEMNO-PR(5) ITEMNAME-PR(15) PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC ON-HAND-PR(6) X. X(5). X(3). X(15). X(3). ZZ,ZZ9. X(3). ZZ,ZZ9. X(3). ZZ,ZZ9. X(3). ZZZ.99. X(3). Z,ZZ.99. X(3). XXX. X(3). REORD-PT-PR(6) ON-ORDER-PR(6) PRICE-PR(6) COST-PR(6) VENDORID-PR(3) 01 TOTAL-LINE (set up in WORKING-STORAGE) 01 TOTAL-LINE. 05 FILLER VALUE " TOTAL RECORDS: 05 TOTAL-RECORDS-ACC-TL 05 FILLER 05 TOTAL-ON-HAND-ACC-TL 05 FILLER 05 TOTAL-ON-ORDER-ACC-TL 05 FILLER PIC ". PIC PIC PIC PIC PIC PIC X(16) This slide lays out the TOTAL-LINE to be compatible with the detail line called PRINTZ. ZZ9. X(7) ZZZ,ZZ9. XX ZZZ,ZZ9. X(38) VALUE SPACES. VALUE SPACES. VALUE SPACES. PRINTZ ITEMNO-PR(5) ON-HAND-PR(6) ITEMNAME-PR(15) REORD-PT-PR(6) ON-ORDER-PR(6) TOTAL-LINE ^TOTAL^RECORDS:^ TOTAL-ON-HAND-ACC-TL(7) TOTAL-RECORDS-ACC-TL(3) TOTAL-ON-ORDER-ACC-TL PRICE-PR(6) COST-PR(6) VENDORID-PR(3) Processing accumulators 01 Adding to accumulators ACCUMULATORS. 05 TOTAL-RECORDS-ACC 05 TOTAL-ON-HAND-ACC 05 TOTAL-ON-ORDER-ACC PIC 999 PIC 9(6) PIC 9(6) B-200-PROCESS-RECORD-LOOP. MOVE SPACES TO PRINTZ. MOVE ITEMNO TO ITEMNO-PR. MOVE ITEMNAME TO ITEMNAME-PR. MOVE ON-HAND TO ON-HAND-PR. MOVE ON-ORDER TO ON-ORDER-PR. MOVE REORD-PT TO REORD-PT-PR. MOVE COST TO COST-PR. MOVE PRICE TO PRICE-PR. MOVE VENDORID TO VENDORID-PR. ADD 1 TO TOTAL-RECORDS-ACC. ADD ON-HAND TO TOTAL-ON-HAND-ACC. ADD ON-ORDER TO TOTAL-ON-ORDER-ACC WRITE PRINTZ AFTER ADVANCING 1 LINE. READ INVEN-FILE AT END MOVE "YES" TO EOF-IND. VALUE 0. VALUE 0. VALUE 0. Part of processing a record is to add to the accumulators. This assures that the information about each record will be included in the accumulators. Setting up and writing TOTAL-LINE Setting up totals on total line 01 01 ACCUMULATORS. 05 TOTAL-RECORDS-ACC 05 TOTAL-ON-HAND-ACC 05 TOTAL-ON-ORDER-ACC TOTAL-LINE. 05 FILLER VALUE " TOTAL RECORDS: 05 TOTAL-RECORDS-ACC-TL 05 FILLER 05 TOTAL-ON-HAND-ACC-TL 05 FILLER 05 TOTAL-ON-ORDER-ACC-TL 05 FILLER PIC 999 PIC 9(6) PIC 9(6) PIC ". PIC PIC PIC PIC PIC PIC VALUE 0. VALUE 0. VALUE 0. X(16) ZZ9. X(7) ZZZ,ZZ9. XX ZZZ,ZZ9. X(38) VALUE SPACES. VALUE SPACES. VALUE SPACES. C-100-TERMINATE. MOVE TOTAL-RECORDS-ACC TO TOTAL-RECORDS-ACC-TL. The final total line is MOVE TOTAL-ON-HAND-ACC TO TOTAL-ON-HAND-ACC-TL. written by moving it MOVE TOTAL-ON-ORDER-ACC TO TOTAL-ON-ORDER-ACC-TL. to PRINTZ and WRITE PRINTZ FROM TOTAL-LINE writing PRINTZ. AFTER ADVANCING 2 LINES. The move statements move the The from CLOSE INVEN-FILE information from the accumulators to accomplishes the INVEN-REPORT. the final total line. move. Flowchart - final totals - part 1 MAINLINE PERFORM A-100INITIALIZE PERFORM B-100PROCESS PERFORM C-100WRAPUP A-100INITIALIZE OPEN files End INITIALIZE B-100PROCESS READ a record EOF Move NO to MORE-RECS N MORE-RECS Not NO N STOP RUN Y End PROCESS Y PERFORM B-200LOOP Flowchart - final totals - part 2 C-100 WRAPUP B-200-LOOP Set up line Move accumulators to total line ADD to accumulators WRITE total line WRITE detail line CLOSE files READ a record EOF N End LOOP Y Move NO to MORE-RECS End WRAPUP Program - part 1 IDENTIFICATION DIVISION. PROGRAM-ID. GRO1. AUTHOR. GROCER. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INVEN-FILE ASSIGN TO "C:\PCOBWIN\CIS12FST\INVEN.DAT". SELECT INVEN-REPORT ASSIGN TO PRINTER. DATA DIVISION. FILE SECTION. FD INVEN-FILE DATA RECORD IS INVEN-REC. 01 INVEN-REC. 05 ITEMNO PIC X(5). 05 ITEMNAME PIC X(15). 05 ON-HAND PIC 9(5). 05 ON-ORDER PIC 9(5). 05 REORD-PT PIC 9(5). 05 COST PIC 999V99. 05 PRICE PIC 9(4)V99. 05 FILLER PIC X(10). 05 VENDORID PIC XXX. Program - part 2 FD 01 INVEN-REPORT DATA RECORD IS PRINTZ. PRINTZ. 05 FILLER 05 ITEMNO-PR 05 FILLER 05 ITEMNAME-PR 05 FILLER 05 ON-HAND-PR 05 FILLER 05 ON-ORDER-PR 05 FILLER 05 REORD-PT-PR 05 FILLER 05 COST-PR 05 FILLER 05 PRICE-PR 05 FILLER 05 VENDORID-PR 05 FILLER PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC X. X(5). X(3). X(15). X(3). ZZ,ZZ9. X(3). ZZ,ZZ9. X(3). ZZ,ZZ9. X(3). ZZZ.99. X(3). Z,ZZ.99. X(3). XXX. X(3). Program - part 3 WORKING-STORAGE SECTION. 01 INDICATORZ. 05 EOF-IND PIC XXX VALUE SPACES. 01 ACCUMULATORS. 05 TOTAL-RECORDS-ACC PIC 999 VALUE 0. 05 TOTAL-ON-HAND-ACC PIC 9(6) VALUE 0. 05 TOTAL-ON-ORDER-ACC PIC 9(6) VALUE 0. 01 TOTAL-LINE. 05 FILLER PIC X(16) VALUE " TOTAL RECORDS: ". 05 TOTAL-RECORDS-ACC-TL PIC ZZ9. 05 FILLER PIC X(7) VALUE SPACES. 05 TOTAL-ON-HAND-ACC-TL PIC ZZZ,ZZ9. 05 FILLER PIC XX VALUE SPACES. 05 TOTAL-ON-ORDER-ACC-TL PIC ZZZ,ZZ9. 05 FILLER PIC X(38) VALUE SPACES. Program - part 4 PROCEDURE DIVISION. MAIN-PROGRAM. PERFORM A-100-INITIALIZE. PERFORM B-100-PROCESS-FILE. PERFORM C-100-TERMINATE. STOP RUN. A-100-INITIALIZE. OPEN INPUT INVEN-FILE OUTPUT INVEN-REPORT. B-100-PROCESS-FILE. READ INVEN-FILE AT END MOVE "YES" TO EOF-IND. PERFORM B-200-PROCESS-RECORD-LOOP UNTIL EOF-IND = "YES". Program - part 5 B-200-PROCESS-RECORD-LOOP. MOVE SPACES TO PRINTZ. MOVE ITEMNO TO ITEMNO-PR. MOVE ITEMNAME TO ITEMNAME-PR. MOVE ON-HAND TO ON-HAND-PR. MOVE ON-ORDER TO ON-ORDER-PR. MOVE REORD-PT TO REORD-PT-PR. MOVE COST TO COST-PR. MOVE PRICE TO PRICE-PR. MOVE VENDORID TO VENDORID-PR. ADD 1 TO TOTAL-RECORDS-ACC. ADD ON-HAND TO TOTAL-ON-HAND-ACC. ADD ON-ORDER TO TOTAL-ON-ORDER-ACC WRITE PRINTZ AFTER ADVANCING 1 LINE. READ INVEN-FILE AT END MOVE "YES" TO EOF-IND. C-100-TERMINATE. MOVE TOTAL-RECORDS-ACC TO TOTAL-RECORDS-ACC-TL. MOVE TOTAL-ON-HAND-ACC TO TOTAL-ON-HAND-ACC-TL. MOVE TOTAL-ON-ORDER-ACC TO TOTAL-ON-ORDER-ACC-TL. WRITE PRINTZ FROM TOTAL-LINE AFTER ADVANCING 2 LINES. CLOSE INVEN-FILE INVEN-REPORT. Show input data description and the actual data on the file. FD 01 INVEN-FILE DATA RECORD IS INVEN-REC. INVEN-REC. 05 ITEMNO PIC 05 ITEMNAME PIC 05 ON-HAND PIC 05 ON-ORDER PIC 05 REORD-PT PIC 05 COST PIC 05 PRICE PIC 05 FILLER PIC 05 VENDORID PIC X(5). X(15). 9(5). 9(5). 9(5). 999V99. 9(4)V99. X(10). XXX. Input data file: 11111TEDDY BEAR 00100000300007501500001999 12121DOLL 00043000000010001400001795 12345TRUCK 00031000250008000500000898 22222GOODNIGHT MOON 00052000000009000800001199 24242ADVEN PETER RAB00027000250006000900001798 27282LITTLE WOMEN 00040000000005001500001797 30303LASSIE COMEHOME00087000250005001200015097 33457BUILDING BLOCKS00100002000025000900001275 40000CRIB 00023000000005010000012500 45678BOUNCE BALL 01000005000075000300000498 ABC XYZ XYZ ABC ABC ABC ABC XYZ XYZ Detail line and output FD 01 11111 12121 12345 22222 24242 27282 30303 33457 40000 45678 INVEN-REPORT DATA RECORD IS PRINTZ. PRINTZ. 05 FILLER 05 ITEMNO-PR 05 FILLER 05 ITEMNAME-PR 05 FILLER 05 ON-HAND-PR 05 FILLER 05 ON-ORDER-PR 05 FILLER 05 REORD-PT-PR 05 FILLER 05 COST-PR 05 FILLER 05 PRICE-PR 05 FILLER 05 VENDORID-PR 05 FILLER TEDDY BEAR DOLL TRUCK GOODNIGHT MOON ADVEN PETER RAB LITTLE WOMEN LASSIE COMEHOME BUILDING BLOCKS CRIB BOUNCE BALL 100 43 31 52 27 40 87 100 23 1,000 PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC PIC X. X(5). X(3). X(15). X(3). ZZ,ZZ9. X(3). ZZ,ZZ9. X(3). ZZ,ZZ9. X(3). ZZZ.99. X(3). Z,ZZ.99. X(3). XXX. X(3). 30 0 25 0 25 0 25 200 0 500 75 100 80 90 60 50 50 250 50 750 15.00 14.00 5.00 8.00 9.00 15.00 12.00 9.00 100.00 3.00 19.99 17.95 8.98 11.99 17.98 17.97 1,50.97 12.75 1,25.00 4.98 ABC XYZ XYZ ABC ABC ABC ABC XYZ XYZ TOTAL-LINE Layout of total line and total line itself 01 TOTAL-LINE. 05 FILLER VALUE " TOTAL RECORDS: 05 TOTAL-RECORDS-ACC-TL 05 FILLER 05 TOTAL-ON-HAND-ACC-TL 05 FILLER 05 TOTAL-ON-ORDER-ACC-TL 05 FILLER TOTAL RECORDS: 10 PIC ". PIC PIC PIC PIC PIC PIC X(16) ZZ9. X(7) ZZZ,ZZ9. XX ZZZ,ZZ9. X(38) 1,503 VALUE SPACES. VALUE SPACES. VALUE SPACES. 805 Input file and Output Report Input data file: 11111TEDDY BEAR 00100000300007501500001999 12121DOLL 00043000000010001400001795 12345TRUCK 00031000250008000500000898 22222GOODNIGHT MOON 00052000000009000800001199 24242ADVEN PETER RAB00027000250006000900001798 27282LITTLE WOMEN 00040000000005001500001797 30303LASSIE COMEHOME00087000250005001200015097 33457BUILDING BLOCKS00100002000025000900001275 40000CRIB 00023000000005010000012500 45678BOUNCE BALL 01000005000075000300000498 ABC XYZ XYZ ABC ABC ABC ABC XYZ XYZ Output file: 11111 12121 12345 22222 24242 27282 30303 33457 40000 45678 TEDDY BEAR DOLL TRUCK GOODNIGHT MOON ADVEN PETER RAB LITTLE WOMEN LASSIE COMEHOME BUILDING BLOCKS CRIB BOUNCE BALL TOTAL RECORDS: 10 100 43 31 52 27 40 87 100 23 1,000 30 0 25 0 25 0 25 200 0 500 1,503 805 75 100 80 90 60 50 50 250 50 750 15.00 14.00 5.00 8.00 9.00 15.00 12.00 9.00 100.00 3.00 19.99 17.95 8.98 11.99 17.98 17.97 1,50.97 12.75 1,25.00 4.98 ABC XYZ XYZ ABC ABC ABC ABC XYZ XYZ