Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
← Back to Debugging
Debugging | Intermediate | COBOL

Fix Sign Handling

Problem Description

Negative number displays wrong:
01 WS-BAL PIC 9(5) VALUE 0.
SUBTRACT 100 FROM WS-BAL.

Expected Output

Should show -100, shows garbage

Hints

Use signed PIC for fields that can be negative.

Solution

IDENTIFICATION DIVISION. PROGRAM-ID. FIXSIGN. DATA DIVISION. WORKING-STORAGE SECTION. * BUG: PIC 9(5) cannot hold negative values * FIX: Use S9(5) for signed numeric 01 WS-BAL PIC S9(5) VALUE 0. 01 WS-DISP PIC -(5)9. PROCEDURE DIVISION. SUBTRACT 100 FROM WS-BAL. MOVE WS-BAL TO WS-DISP. DISPLAY "BALANCE: " WS-DISP. STOP RUN.

Explanation:

Use S (sign) in PIC clause for fields that can be negative: PIC S9(5).