Cindy Wilson
Cindy Wilson

Reputation: 1

CIty-data cannot be subscripted -Error compiling cobol program

   IDENTIFICATION DIVISION.
   PROGRAM-ID. Assignment00.

   DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 Temperature PIC 9(3)V9(1).
   01 Counter PIC 9(2) VALUE 1.
   01 City-Names.
      05 City-Name OCCURS 5 TIMES PIC X(10) VALUE 
         'Montreal', 'Ottawa', 'Toronto', 'Kingston', 'Cornwall'.
   01 City-Temps.
      05 City-Temps-Array OCCURS 5 TIMES
         INDEXED BY City-Index.
         10 Temp OCCURS 5 TIMES PIC 9(3)V9(1).
   01 Total-Temperature PIC 9(5)V9(1).
   01 Average-Temperature PIC 9(3)V9(1).

   PROCEDURE DIVISION.
   MAIN-LOGIC.
       PERFORM VARYING City-Index FROM 1 BY 1 UNTIL City-Index > 5
         DISPLAY "ENTER the last 5 daily high temperatures for: " 
         City-Names(City-Index)
         PERFORM ACCEPT-TEMPERATURES
         PERFORM CALCULATE-AVERAGE
         DISPLAY "Average temperature for " 
         City-Names(City-Index) " is " Average-Temperature "C"
       END-PERFORM
       STOP RUN.

   ACCEPT-TEMPERATURES.
       PERFORM VARYING Counter FROM 1 BY 1 UNTIL Counter > 5
         DISPLAY "Enter temperature #" Counter ": "
         ACCEPT Temperature
         MOVE Temperature TO Temp(Counter) OF City-Temps(City-Index)
       END-PERFORM.

   CALCULATE-AVERAGE.
       COMPUTE Total-Temperature = 0
       PERFORM VARYING Counter FROM 1 BY 1 UNTIL Counter > 5
          ADD Temp(Counter) OF City-Temps(City-Index) TO 
          Total-Temperature
       END-PERFORM
       COMPUTE Average-Temperature = Total-Temperature / 5.

I am receiving these error messages when I try to compile in VS studio code.

Assignment00.cbl: in paragraph 'MAIN-LOGIC':
Assignment00.cbl:32: error: 'City-Data' cannot be subscripted
Assignment00.cbl:32: error: syntax error, unexpected :
Assignment00.cbl:31: error: PERFORM statement not terminated by END-PERFORM
Assignment00.cbl: in paragraph 'CITY-DATA-PROCESS':
Assignment00.cbl:42: error: 'City-Data' cannot be subscripted
Assignment00.cbl:42: error: syntax error, unexpected :
Assignment00.cbl:43: error: 'City-Data' cannot be subscripted
Assignment00.cbl:43: error: syntax error, unexpected :

I am trying to create a simple COBOL program using GnuCOBOL. The program has to accept 5 temperatures for each city and then display the average temperature for each city.

I cannot figure out how to resolve this error.

Upvotes: 0

Views: 121

Answers (1)

Simon Sobisch
Simon Sobisch

Reputation: 7288

You defined a group with a "table" of 5 names:

   01 City-Names.
      05 City-Name OCCURS 5 TIMES PIC X(10) VALUE 
         'Montreal', 'Ottawa', 'Toronto', 'Kingston', 'Cornwall'.

but then subscript the group:

         DISPLAY "ENTER the last 5 daily high temperatures for: " 
         City-Names(City-Index)

Change that to the variable that actually has several OCCURenceS: City-Name (City-Index)

Upvotes: 0

Related Questions