ComradeSpaceMan
ComradeSpaceMan

Reputation: 39

Perform until loop in COBOL only reads once, then breaks out despite the condition still being false

still very new to COBOL. I have a simple program that runs to read a file, then read the contents of that file and format it out to another a file. My main problem lies in fact that for some reason, my module will test the condition, see it is false, then execute the statements inside. Afterwards, it'll repeat the process without changing the condition, then just terminate the program. According to the debugger, it doesn't even run the final module, 200-TERM-RECORD, it just ends.

Here is the code for the program

ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL 
       SELECT INVENT-FILE-IN
           ASSIGN TO "C:\TEST.TXT"
               ORGANIZATION IS LINE SEQUENTIAL.
       SELECT INVENT-REPORT-OUT
           ASSIGN TO "C:\INVREPT.TXT"
               ORGANIZATION IS LINE SEQUENTIAL. 

   data division.
   FILE SECTION. 
   FD INVENT-FILE-IN
       RECORD CONTAINS 32 CHARACTERS.
   01 INVENTORY-DATA-INPUT
       05 PART-NUMBER-INPUT PIC 9(5)
       05 PART-NAME-INPUT PIC X(20)
       05 QUANTITY-ON-HAND-INPUT PIC 9(3)
       05 UNIT-PRICE-INPUT PIC 9(4)

   FD INVENT-REPORT-OUT
       RECORD CONTAINS 77 CHARACTERS.
   01 INVENTORY-DATA-OUTPUT
       05 HEADER
           10 PRODUCT-NUMBER PIC A(6)
           10 FILLER PIC A(1)
           10 PRODUCT-NAME PIC A(8)
           10 FILLER PIC A(16)
           10 PRODUCT-QTY PIC A(3)
           10 FILLER PIC A(3)
           10 PRODUCT-VALUE PIC A(5)
           10 FILLER PIC A(3)
       05 PART-NUMBER-OUTPUT PIC 9(5)
       05 PART-NAME-OUTPUT PIC X(20)
       05 QUANTITY-ON-HAND-OUTPUT PIC 9(3)
       05 UNIT-PRICE-OUTPUT PIC 9(4)
       05 SUPPLIER-CODE-OUTPUT PIC X(5)
       05 RE-ORDER-POINT-OUTPUT PIC 9(3)
       05 RECORD-COUNTER-OUTPUT PIC 9(2)
   working-storage section.
   01 VALUES-AND-TOTALS
       05 EOF-SWITCH PIC X(1) VALUE "N"
       05 TOTAL-VALUE PIC 9(6) VALUE ZERO
       05 INVENTORY-VALUE PIC 9(6) VALUE ZERO 
       05 RECORD-COUNTER PIC 9(2) VALUE ZERO

   procedure division.
   100-CREATE-INVENTORY-RECORD.
       PERFORM 200-INIT-CREATE-INV-RECORD.
       PERFORM 200-CREATE-RECORD 
           UNTIL EOF-SWITCH = "Y".
       PERFORM 200-TERM-RECORD.



   200-INIT-CREATE-INV-RECORD.
       PERFORM 700-OPEN-INV-FILE.
       PERFORM 700-READ-FILE.

   200-CREATE-RECORD.
       PERFORM 700-WRITE-RECORD.
       PERFORM 700-READ-FILE.


   200-TERM-RECORD.
       CLOSE INVENT-FILE-IN.
       CLOSE INVENT-REPORT-OUT.

   700-OPEN-INV-FILE.
       OPEN INPUT INVENT-FILE-IN.
       OPEN OUTPUT INVENT-REPORT-OUT.

   700-READ-FILE.
       READ INVENT-FILE-IN
       AT END MOVE "Y" TO EOF-SWITCH.


   700-WRITE-RECORD.
       MOVE PART-NUMBER-INPUT TO PART-NUMBER-OUTPUT.
       MOVE PART-NAME-INPUT TO PART-NAME-OUTPUT.
       MOVE QUANTITY-ON-HAND-INPUT TO QUANTITY-ON-HAND-OUTPUT.
       MOVE UNIT-PRICE-INPUT TO UNIT-PRICE-OUTPUT.
       WRITE INVENTORY-DATA-OUTPUT.

       goback.

   end program Project2.

Upvotes: 0

Views: 1613

Answers (2)

Rick Smith
Rick Smith

Reputation: 4407

Move the goback statement from the end of the program to the end of the first paragraph.

In 700-WRITE-RECORD, when control reaches the goback statement the program terminates. You likely want the program to terminate at the end of 100-CREATE-INVENTORY-RECORD.

Upvotes: 3

Simon Sobisch
Simon Sobisch

Reputation: 7287

Your code flow (from 100-create on ) is:

  • 200-init
    • 700-open
    • 700-read
  • 200-create
    • 700-write -> goback

no loop involved...

in general

I'd advise to use SECTION and only do GOBACK in either your main SECTION or in a prog-end SECTION that is called from your main (and may be called in exceptional cases, too).

Upvotes: 1

Related Questions