CR and DB
Part of the Data and Numbers section of Coddy's COBOL Programming journey. Lesson 11 of 23.
An edited picture with no sign symbol prints a number without its sign: -12.50 and 12.50 look the same.
Accounts mark negative amounts with CR (credit) or DB (debit) after the number. The two letters print only when the value is negative, and are spaces otherwise:
01 BAL-OUT PIC $$$9.99CR.
MOVE -50 TO BAL-OUT. *> " $50.00CR"
MOVE 60 TO BAL-OUT. *> " $60.00 "A - at the end prints a minus after a negative number and a space after a positive one. A + prints the sign either way:
01 CHG-OUT PIC ZZ9-.
MOVE -5 TO CHG-OUT. *> " 5-"Signs float like $: ---9 prints -5 as -5 and +++9 prints 5 as +5, next to the digits.
The sending field needs an S in its picture. Without one, the value was never negative.
Challenge
EasyA payment bigger than the balance leaves the customer in credit, but BAL-OUT drops the sign: paying 150 on 100 prints Balance: $50.00. Add CR to its picture so it prints Balance: $50.00CR.
Example input, one line per ACCEPT: 150.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. CREDIT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 BALANCE PIC S9(4)V99 VALUE 100.
01 PAYMENT PIC 9(4)V99.
01 BAL-OUT PIC $$$9.99.
PROCEDURE DIVISION.
ACCEPT PAYMENT.
SUBTRACT PAYMENT FROM BALANCE.
MOVE BALANCE TO BAL-OUT.
DISPLAY "Balance: " BAL-OUT.
STOP RUN.