Juanjo
Juanjo

Reputation: 734

gnuCobol 'EOF' is not defined

I have the following Gnu Cobol code:

   IDENTIFICATION DIVISION.
   PROGRAM-ID. INCOME-TAX-CALCULATOR.

   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
       SELECT EMPLOYEE-FILE
           ASSIGN TO "employees.csv"
           ORGANIZATION IS LINE SEQUENTIAL.

   DATA DIVISION.
   FILE SECTION.
   FD  EMPLOYEE-FILE.
   01  EMPLOYEE-RECORD.
       02  EMP-ID              PIC 9(5).
       02  EMP-NAME            PIC X(30).
       02  EMP-SALARY          PIC 9(7).
       02  EMP-TAX             PIC 9(5).

   WORKING-STORAGE SECTION.
   01  WS-TAX-RATE           PIC 9(4)V99 VALUE 20.

   PROCEDURE DIVISION.
   OPEN INPUT EMPLOYEE-FILE
   PERFORM UNTIL EOF
       READ EMPLOYEE-FILE
           AT END SET EOF TO TRUE
       END-READ
       COMPUTE EMP-TAX = EMP-SALARY * WS-TAX-RATE
       DISPLAY EMP-ID, EMP-NAME, EMP-SALARY, EMP-TAX
   END-PERFORM
   CLOSE EMPLOYEE-FILE
   STOP RUN.

in GnuCobol 3.1.2.0 I get the following error: programName:25: error: 'EOF' is not defined

I have seen similar questions but END PROGRAM does not help.

I use the compiler with the free format like this: cobc -x -F -o programName programName.cbl

Upvotes: 0

Views: 161

Answers (2)

Chris Feltman
Chris Feltman

Reputation: 21

There is a better way to do this:

perform until exit
    read employee-file
        at end 
            exit perform
        not at end
            COMPUTE EMP-TAX = EMP-SALARY * WS-TAX-RATE
            DISPLAY EMP-ID, EMP-NAME, EMP-SALARY, EMP-TAX
   end-read
end-perform
            

I always do my reads this way, there is no need for an EOF flag. Even better, break out the not at end stuff into a paragraph and perform that:

perform until exit
    read employee-file
        at end 
            exit perform
        not at end
            perform compute-and-display
   end-read
end-perform
close employee-file
goback
.

compute-and-display.
    COMPUTE EMP-TAX = EMP-SALARY * WS-TAX-RATE
    DISPLAY EMP-ID, EMP-NAME, EMP-SALARY, EMP-TAX
    exit paragraph
.

The exit paragraph is not needed but I like to use it just to make my intent clear that it is an explicit "return". Also, if you are using GnuCobol, you can just use lower case.

Upvotes: 1

Simon Sobisch
Simon Sobisch

Reputation: 7287

You've missed a definition for that EOF, so it is... undefined.

adding a definition like the following in WORKING-STORAGE-SECTION would solve that:

   01  FILLER PIC X VALUE ' '.
       88  EOF VALUE 'E'.

but you'd likely define a "NOT-EOF" or similar and set it before that loop, or, even better get rid of it completely:

   PERFORM UNTIL EXIT
       READ EMPLOYEE-FILE
           AT END EXIT PERFORM
       END-READ
       COMPUTE EMP-TAX = EMP-SALARY * WS-TAX-RATE
       DISPLAY EMP-ID, EMP-NAME, EMP-SALARY, EMP-TAX
   END-PERFORM

... but you'd likely still want to add checks for "did the OPEN work", but that's a different question, if at all.

Upvotes: 2

Related Questions