Search code examples
cobolsample

Is there an infinite loop cause in this COBOL code?


i'm new at programming in COBOL so I tried to make a sample calculator, but I couldn't make it work. The calc enter in an infinite loop and I didn't find the reason, I even tried to copy a similar code and didn't work. So, is it a OpenCobolIDE problem or it's the code?

Code:

   IDENTIFICATION DIVISION.
   PROGRAM-ID. Loops.
   ENVIRONMENT DIVISION.

   DATA DIVISION.
   WORKING-STORAGE SECTION.
   77 WSS-FIRSTOP          PIC S9(05)V9(04).
   77 WSS-SECONDOP         PIC S9(05)V9(04).
   77 WSS-RESULT           PIC S9(10)V9(04).
   77 WSS-OPERATOR         PIC A(01).
   77 WSS-CHOICE           PIC X(01).

   PROCEDURE DIVISION.
   LOOP SECTION.
   PERFORM 0001-STARTCALC UNTIL WSS-CHOICE = 2.

   STOP RUN.
   STARTING SECTION.
   0001-STARTCALC.
       DISPLAY "1.- CALCULATOR, 2.- EXIT".
       DISPLAY "ENTER:", ACCEPT WSS-CHOICE.
       IF WSS-CHOICE = 1
           PERFORM 0002-DATAREQUEST
           PERFORM 0003-OPERATIONS
       ELSE IF WSS-CHOICE = 2
           STOP RUN
       ELSE
           DISPLAY "ERROR. INVALID OPTION.".

   0002-DATAREQUEST.
   CHOICE SECTION.
       DISPLAY "CALCULATOR 1.0".
       DISPLAY "ENTER 1ST OPERATOR:".
       ACCEPT WSS-FIRSTOP.
       DISPLAY "ENTER 2ND OPERATOR".
       ACCEPT WSS-SECONDOP.
       DISPLAY "CHOOSE OPERATION TO PERFORM:".
       DISPLAY "A: ADD, S: SUBTRACT, M: MULTIPLY, D: DIVISION".
       ACCEPT WSS-OPERATOR.

   0003-OPERATIONS.
   OPERATIONS SECTION.
       IF WSS-OPERATOR =                   "A"
           ADD WSS-FIRSTOP TO WSS-SECONDOP GIVING WSS-RESULT
           DISPLAY "ADDITION RESULT: ",    WSS-RESULT
       ELSE IF WSS-OPERATOR =              "S"
           SUBTRACT WSS-FIRSTOP FROM WSS-SECONDOP GIVING WSS-RESULT
           DISPLAY "SUBTRACTING RESULT: ", WSS-RESULT
       ELSE IF WSS-OPERATOR =              "M"
           MULTIPLY WSS-FIRSTOP BY WSS-SECONDOP GIVING WSS-RESULT
           DISPLAY "MULTIPLYING RESULT: ", WSS-RESULT
       ELSE IF WSS-OPERATOR =              "D"
           DIVIDE WSS-SECONDOP INTO WSS-FIRSTOP GIVING WSS-RESULT
           DISPLAY "DIVIDING RESULT: ",    WSS-RESULT
       ELSE
           DISPLAY "INVALID OPERATOR. CHECK".

Solution

  • The calc enter in an infinite loop [...], is it a OpenCobolIDE problem or it's the code?

    I think it is both. The main reason for an infinite loop that may happens without any display is very likely that OCIDE does not have an appropriate terminal. Running the same on a separate terminal should lead at least to an display + accept before you get to the loop. If I remember correctly there are options in OCIDE's preferences to adjust the way the terminal behaves.

    The reason that the code "looks" like an infinite loop is (as @bruce-martin mentioned) the wrong use of paragraphs and sections. If you want to keep both, you need to place the section headers before a paragraph as the paragraph ends if either a new paragraph is defined or a section is defined.

    Note: The use of sections and paragraphs is often company-/team-defined: if it isn't (as in any programming language) and you edit an existing program: use whatever the old code does - for new code: do as you like. (My personal preference is to only use paragraphs when I want to GOTO them and to always use EXIT SECTION to leave a section instead of a GOTO mysec-end)

    To solve possible issues: use either literals '1' or a numeric WSS-CHOICE PIC 9 [for a terminal i/o I'd always prefer alphanumic + literal]. As you've mentioned you're looking at COBOL the first time: definitely check out EVALUATE WSS-OPERATOR.