I am trying to read 3 lines of input from COBOL STDIN and store them to 3 different variables. I plan on using COMPUTE on these inputs to perform a calculation.
I have been looking at TutorialsPoint (I'm teaching myself how to code COBOL), but to no avail.
Below is the code I have so far:
IDENTIFICATION DIVISION.
PROGRAM-ID. SOLUTION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT SYSIN ASSIGN TO KEYBOARD ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD SYSIN.
01 INPUT-LINE PIC X(255).
88 EOF VALUE HIGH-VALUES.
WORKING-STORAGE SECTION.
01 MEAL-COST PIC 9(2)V9(2).
01 TIP-PERCENT PIC 9(3).
01 TAX-PERCENT PIC 9(3).
PROCEDURE DIVISION.
OPEN INPUT SYSIN
READ SYSIN
AT END SET EOF TO TRUE
END-READ
DISPLAY MEAL-COST
DISPLAY TIP-PERCENT
DISPLAY TAX-PERCENT
CLOSE SYSIN.
STOP RUN.
The inputs will be of form:
12.00
5
20
And the result will be an integer (in the sense of other programming languages).
The only way I see storing the variables is to, upon each sequential iteration, have a counter and store it to each variable (MEAL-COST, TIP-PERCENT, TAX-PERCENT). However, perhaps there is an easier way to do this?
This is what I've done, but I believe there is yet a better way to get the input and store them into the correct variables. If anyone has any suggestions, that would be much appreciated.
IDENTIFICATION DIVISION.
PROGRAM-ID. SOLUTION.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT SYSIN ASSIGN TO KEYBOARD ORGANIZATION LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD SYSIN.
01 INPUT-LINE PIC X(255).
88 EOF VALUE HIGH-VALUES.
WORKING-STORAGE SECTION.
01 MEAL-COST PIC 9(3)V9(2).
01 TIP-PERCENT PIC 9(3)V9(2).
01 TAX-PERCENT PIC 9(3)V9(2).
01 TIP PIC 9(3)V9(2).
01 TAX PIC 9(3)V9(2).
01 TOTAL-COST PIC 9(3)V9(2).
01 ROUNDED-TOTAL-COST PIC 9(3).
01 FORMATTED-RESULT PIC Z(3).
PROCEDURE DIVISION.
OPEN INPUT SYSIN
READ SYSIN
AT END SET EOF TO TRUE
NOT AT END
MOVE INPUT-LINE TO MEAL-COST
READ SYSIN
AT END SET EOF TO TRUE
NOT AT END
MOVE INPUT-LINE TO TIP-PERCENT
READ SYSIN
AT END SET EOF TO TRUE
NOT AT END
MOVE INPUT-LINE TO TAX-PERCENT
END-READ.
COMPUTE TIP= (MEAL-COST * TIP-PERCENT / 100).
COMPUTE TAX= (MEAL-COST * TAX-PERCENT / 100).
COMPUTE TOTAL-COST= MEAL-COST + TIP + TAX.
COMPUTE ROUNDED-TOTAL-COST ROUNDED = TOTAL-COST.
MOVE ROUNDED-TOTAL-COST TO FORMATTED-RESULT.
DISPLAY "The total meal cost is" FORMATTED-RESULT " dollars.".
CLOSE SYSIN.
STOP RUN.
You don't need to OPEN/CLOSE your SYSIN.
Just code: