5

Why can I not do this nested perform loop in COBOL?

If I put END-PERFORM. in any line sooner than where I have the last one just before EXIT PROGRAM - it works. But I need the program to display the INPUT C value every time. in the outer perform loop. Its driving me nuts.

PROCEDURE DIVISION USING INPUTC CIPHER.
COMPUTE CIPHERMAX = CIPHER.
MULTIPLY -1 BY CIPHER
---> PERFORM VARYING CIPHER FROM 0 BY 1
UNTIL CIPHERMAX = CIPHER
    DISPLAY 'This is loop number: ' CIPHER
    INSPECT INPUTC CONVERTING
    "avcdefghijklmnopqrstuvwxyz" to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    COMPUTE CONVERTNUM = FUNCTION MOD (CIPHER, 26)
    INSPECT FUNCTION REVERSE(INPUTC) TALLYING LENGTHNUM FOR LEADING SPACES
    COMPUTE LENGTHNUM = LENGTH OF CIPHER - LENGTHNUM

 ---> PERFORM UNTIL SENTRY = LENGTHNUM

            IF ((FUNCTION ORD(INPUTC(SENTRY:1)) + CONVERTNUM) > (FUNCTION ORD('Z')))
             MOVE FUNCTION CHAR((FUNCTION ORD(INPUTC(SENTRY:1)) + CONVERTNUM) - 26) TO RECHAR
              ELSE
             MOVE FUNCTION CHAR(FUNCTION ORD(INPUTC(SENTRY:1)) + CONVERTNUM) TO RECHAR
             END-IF  
              IF (((FUNCTION ORD(INPUTC(SENTRY:1))) >= (FUNCTION ORD('A'))) AND 
             ((FUNCTION ORD(INPUTC(SENTRY:1))) <= (FUNCTION ORD('Z'))))
             IF ((FUNCTION ORD(INPUTC(SENTRY:1)) + CONVERTNUM) > (FUNCTION ORD('Z')))
                INSPECT INPUTC(SENTRY:1) REPLACING ALL INPUTC(SENTRY:1) BY RECHAR
             ELSE
                INSPECT INPUTC(SENTRY:1) REPLACING ALL INPUTC(SENTRY:1) BY RECHAR
             END-IF
          ELSE
                INSPECT INPUTC(SENTRY:1) REPLACING ALL INPUTC(SENTRY:1) BY INPUTC(SENTRY:1)
          END-IF

          COMPUTE SENTRY = SENTRY + 1
        --->  END-PERFORM
    DISPLAY INPUTC.
    COMPUTE LOOPI = LOOPI + 1
    --->END-PERFORM.
EXIT PROGRAM.
END PROGRAM SOLVE.
user99999991
  • 1,217
  • 3
  • 17
  • 35
  • 1
    You have an IF with its ELSE having identical code. You also have an ELSE with an INSPECT replacing a character by itself. Why are you using INSPECT ... REPLACING for one-byte fields? Why not just MOVE? What is LOOPI for? Why not ADD 1 TO SENTRY instead of COMPUTE? You MULTIPLY -1, SUBTRACT ... FROM ZERO will be faster. But then you immediately set CIPHER to 0 in the PERFORM, so why negate anyway? I suspect there is more, but you've made the whole thing difficult to follow with FUNCTION upon FUNCTION. – Bill Woodger Apr 26 '13 at 00:02
  • 2
    Also, in `avcdefghijklmnopqrstuvwxyz`, you may have meant `abc`... – Dan Lugg Jul 17 '13 at 14:06

2 Answers2

9

That nasty scope terminating period after DISPLAY INPUTC. is terminating the scope of the nested PERFORM statements. Get rid of the period and all should work fine.

The only periods you should ever use in the Procedure Division when coding a program to COBOL-85 standard are the ones required to terminate section and paragraph headers and another one to terminate the current paragraph, section or program.

NealB
  • 15,862
  • 2
  • 34
  • 60
4

You have a period in the display INPUTC statement. Remove the period and you should be ok. The Period always ends a statement.

user99999991
  • 1,217
  • 3
  • 17
  • 35
Baruch Atta
  • 401
  • 1
  • 4
  • 14