An Error Report
Part of the Programs That Work Together section of Coddy's COBOL Programming journey. Lesson 13 of 16.
A batch program that meets a bad record should not stop the run, and should not skip it in silence either. It checks each record, processes the good ones and lists the bad ones in an error report that someone fixes before the next run.
EVALUATE TRUE runs the checks in order and stops at the first that fails, so each bad record gets one clear reason:
EVALUATE TRUE
WHEN TR-ID IS NOT NUMERIC
MOVE "Bad id" TO ERR-MSG
WHEN NOT VALID-CODE
MOVE "Bad code" TO ERR-MSG
WHEN OTHER
MOVE SPACES TO ERR-MSG
END-EVALUATEOrder matters. Test that an amount is numeric before any test of its value. GnuCOBOL compares bad digits without stopping, but the answer means nothing: an amount of spaces is equal to zero.
The report ends with counts, so whoever reads it sees at once how much of the file went through:
DISPLAY "Accepted " ACCEPTED ", rejected " REJECTEDChallenge
EasyThe checks catch a bad id and a bad code. Add two more after them, so record 4 prints Record 4: Bad amount and record 5 Record 5: Zero amount: when TR-AMOUNT is not numeric, and when it is zero.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. CHECKTX.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT TRANS-FILE ASSIGN TO TRANSIN
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD TRANS-FILE.
01 TRANS-REC.
05 TR-CODE PIC X.
88 VALID-CODE VALUE "D" "W".
05 TR-ID PIC 9(5).
05 TR-AMOUNT PIC 9(5)V99.
WORKING-STORAGE SECTION.
01 EOF-FLAG PIC X VALUE "N".
88 END-OF-FILE VALUE "Y".
01 REC-NO PIC 9 VALUE ZERO.
01 ACCEPTED PIC 9 VALUE ZERO.
01 REJECTED PIC 9 VALUE ZERO.
01 ERR-MSG PIC X(12).
01 TOTAL PIC 9(6)V99 VALUE ZERO.
01 SHOW-TOTAL PIC ZZZ,ZZ9.99.
PROCEDURE DIVISION.
MAIN-LOGIC.
DISPLAY "Error report"
OPEN INPUT TRANS-FILE
PERFORM UNTIL END-OF-FILE
READ TRANS-FILE
AT END SET END-OF-FILE TO TRUE
NOT AT END PERFORM CHECK-RECORD
END-READ
END-PERFORM
CLOSE TRANS-FILE
MOVE TOTAL TO SHOW-TOTAL
DISPLAY "Accepted " ACCEPTED
", rejected " REJECTED
DISPLAY "Accepted total: " SHOW-TOTAL
STOP RUN.
CHECK-RECORD.
ADD 1 TO REC-NO
EVALUATE TRUE
WHEN TR-ID IS NOT NUMERIC
MOVE "Bad id" TO ERR-MSG
WHEN NOT VALID-CODE
MOVE "Bad code" TO ERR-MSG
*> Check the amount here
WHEN OTHER
MOVE SPACES TO ERR-MSG
END-EVALUATE
IF ERR-MSG = SPACES
ADD 1 TO ACCEPTED
ADD TR-AMOUNT TO TOTAL
ELSE
ADD 1 TO REJECTED
DISPLAY "Record " REC-NO ": " ERR-MSG
END-IF.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.