Final Totals in COBOL
This will show the processing necessary to produce a final total line on a simple read/write program using COBOL.
These are the tasks that must be accomplished to handle final total processing.
This shows the WORKING-STORAGE SECTION for this final total program. Notice that there are three accumulators and three places to print the accumulators on the TOTAL-LINE. In the PROCEDURE DIVISION, when end of file has been reached, we will move the accumulators to the final total line.
Please notice that the PIC on the accumulators is compatible with the PIC on the fields in the final total line.
One other note on processing, as records are processed amounts are added to the accumulators.
COBOL RULE: VALUE clauses are not used with edited fields in WORKING-STORAGE.
This is the print line set up in the FILE SECTION. Before we set up the total line, we need to examine the layout of the print line.
This total line will line up under the detail line described in PRINTZ on the previous slide. That means that the total for the on hand field will be under the on hand column and the total for the on order field will be under the on order column.
In the routine that processes the records (in this program it is called B-200-PROCESS-RECORD-LOOP), the totals are added to the accumulators.
In the C-100-TERMINATE routine, the final total line gets written. Since all Input/Output has to pass through the FILE SECTION, the only thing that can be written is PRINTZ (the name defined in the 01 level of the FD for the print file).
WRITE PRINTS FROM TOTAL-LINE means that TOTAL-LINE will be moved to PRINTZ and then PRINTZ will be written. After 2 LINES means ther will be a blank line before the total line.
Since all lines are written out of PRINTZ, PRINTZ should always be established at the maximum size for the printer being used.
This is a flowchart of the logic involved in final total processing. The routines shown are:
The MAINLINE which controls the program logic.
The A-100-INITIALIZE which opens the files.
The N-100-PROCEESS which reads the initial record and checks to see if one was found. If a record was found it that checks the UNTIL on the PERFORM to make sure that MORE-RECS has not been set to NO. As long as it has not, then it will PERFORM B-200-PROCESS (shown on next page).
NOTE: MORE-RECS would be initialized to YES in WORKING-STORAGE.
This shows two parts of the flowchart:
The B-200-LOOP is where the records are processed and the adding to the accumulators is done.
The C-100-WRAPUP is where the totals are moved to the total line, the total line is written and the files are closed.
Environment Division through the input file description
Continuation of the FILE SECTION with print file description.
WORKING-STORAGE section containing end of file indicator, accumulators and final total line.
Beginning of the PROCEDURE DIVISION.
Paragraphs in the PROCEDURE DIVISION that process records and process totals.
This slide shows the input data description from the FILE SECTION and the actual data that is being read in off the file.
This shows the print line description and the data that results.
This shows the layout of the total line and the line on the printed report that results.
The input file was read and the output report was printed.
You can see that each record on the input shows up on the output and that the total line contains the correct totals.
10 records were processed and the sums in the on hand and on order columns add up to the totals shown at the bottom.