A Very Simple COBOL Calculator

8 Jul

So let’s take a look at a COBOL program that can be used as a simple calculator…

Notice the PROCEDURE DIVISION has only one paragraph.  Let’s parse this paragraph line by line and see what its doing.

DISPLAY “COBOL CALCULATOR” LINE 4 POSITION 10 ERASE SCREEN.
On the first line, we’re telling the program to display the words “COBOL CALCULATOR” on line 4, starting in position 10 of the screen.   The words “ERASE SCREEN” cause the screen to be cleared before the display.

DISPLAY “Enter First Number : ” LINE 6 POSITION 10.
On the next line, we’re prompting the user to enter a number.

ACCEPT NUM1-TXT LINE 6 POSITION 31 PROMPT ECHO.
This line will cause the program to pause while the user enters a number.   Note in the WORKING-STORAGE SECTION that the field NUM1-TXT is defined as PIC X(3), which means that the largest number the user can enter is 999.  That wouldn’t work very well for a conventional calculator, but it will serve the purpose for our simple calculator.  Also note that we’ve accepted an alphanumeric field from the user, which will prevent any leading zeroes from being displayed on the screen.   The word PROMPT causes the program to display an underscore the length of the field to be entered, and the word ECHO causes the program to display what was entered back to the user after the Enter key is pressed.

MOVE NUM1-TXT TO NUM1 WITH CONVERSION.
Now we move the alphanumeric field entered above to a numeric field so that we’re able to do calculations on it.   Note that the field NUM1 is defined as PIC 9(3).   The words WITH CONVERSION will convert our alphanumeric field to a numeric field, populating leading zeroes as necessary.

DISPLAY “Enter Operator (+,-,*,/): ” LINE 8 POSITION 10.
This line prompts the user to enter an operator.   Do we want to add, subtract, multiply, or divide?

ACCEPT OPERATOR LINE 8 POSITION 36 PROMPT ECHO.
Program will pause to allow the user to enter an operator, and then press the Enter key.

DISPLAY “Enter Second Number: ” LINE 10 POSITION 10.
ACCEPT NUM2-TXT LINE 10 POSITION 31 PROMPT ECHO.
MOVE NUM2-TXT TO NUM2 WITH CONVERSION.
These lines function the same as the prompt and accept for the first number above.

IF OPERATOR = “+”
    ADD NUM1, NUM2 GIVING RESULT.
IF OPERATOR = “-“
    SUBTRACT NUM2 FROM NUM1 GIVING RESULT.
IF OPERATOR = “*”
    MULTIPLY NUM1 BY NUM2 GIVING RESULT.
IF OPERATOR = “/”
    DIVIDE NUM1 BY NUM2 GIVING RESULT.
We determine how to calculate the result dependent upon the operator entered.  Pretty self explanatory, right?

MOVE RESULT TO RESULT-EDIT.
Note that the field RESULT is defined in WORKING-STORAGE as PIC S9(6)V9(2).    This picture clause defines the field as a signed numeric field, with an implied decimal point, having six places  to the left of the decimal point, and two places to the right of the decimal point.   Also note that NUM1 and NUM2 can both contain numbers up to 999, so the greatest result our calculator can calculate is 998,001  (999 * 999).  Is our picture clause big enough to hold this value?   Yes, it is.   We also added two decimal places to the right of the decimal for division of numbers.    We move the value of RESULT to RESULT-EDIT, which is defined as PIC -ZZZ,ZZ9.99.    This will cause a negative number to appear as signed, and will also prevent the display of leading zeroes.  Each “Z” in a character-string is used to represent the leftmost numeric character position which will be replaced by a space character when the contents of that character position equals zero.  Each “9” in the character-string represents a character position which contains a numeric digit.   The “.” in the character-string is displayed as a decimal point.

DISPLAY NUM1-TXT LINE 12 POSITION 10.
DISPLAY OPERATOR LINE 12 POSITION 14.
DISPLAY NUM2-TXT LINE 12 POSITION 16.
DISPLAY “=” LINE 12 POSITION 20.
DISPLAY RESULT-EDIT LINE 12 POSITION 22.
Now we display our calculation and the result, specifying the exact location on the screen for each field.

And there you have it.    Except that I  bet  you’re seeing a few issues with our calculator. First of all, since we’re accepting our numeric values as alphanumeric fields from the user in order to avoid the display of leading zeroes, how can we be sure that the user is entering a numeric value?   He could just as easily enter XXX, and that would definitely confuse our calculator.  How can we force a numeric value to be entered?   Also, how can we be sure that the operator entered is one of the four designated choices?

There’s a number of ways to accomplish this, and every programmer has their own methods.   Here’s my method:

Let’s change the PROCEDURE DIVISON to this:

Ah, now that’s better.   We’ve moved our three ACCEPT statement to individual paragraphs, and we’ll PERFORM those to get the desired results.  We’ll check each position of NUM1-TXT and NUM2-TXT looking for at least one numeric character, and move only numeric characters to our numerically defined fields.   If no numeric characters are detected, we’ll prompt the user again until he enters numeric data.   For our operator, we’ll check to make sure the user entered one of the four operators we’re looking for.   If not, we’ll prompt again until he gets it right.    While the goal is to make your programs as user-friendly as possible, adding logic to perform checks such as these can save time and frustration for both you and the user.    And we live to make our users happy, right?

 

Leave a Reply

Your email address will not be published.