I've been getting into COBOL lately, and I find it a very peculiar and interesting language that can still hold up to this day.
The reason I'm uploading this is mostly to get some feedback in code organisation and conventions, as well as in optimal uses for subroutines, since I had a hard time trying to reuse some of them and avoid copying pieces of code several times. Thus any professional advice or insight into what's wrong with this is welcome (I sincerely appreciate critiques!).
This is basically a database where a user can store up to 100 subjects of 5 exams each. The program calculates the average of each subject, as well as a general average of all subjects entered. It assigns an id to each subject by increasing a counter, which never resets.
The program is also capable of retiring subjects from the table by zeroing their camps. When adding another subject, it will search for zeroed ids and store the new subject in their camps.
I must admit it is a pretty primitive program, but I made it for learning purposes only.
IDENTIFICATION DIVISION.
PROGRAM-ID. SUBJECTS-DATA-BASE.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 WK-MAX-SUBJECTS PIC 9(03) VALUE 0.
77 WK-LAST-SUBJECT-ADDED PIC 9(03) VALUE 0.
77 WK-ID-COUNTER PIC 9(03) VALUE 0.
77 WK-SEARCH-ID PIC 9(03) VALUE 0.
77 WK-END-PROGRAM PIC 9(01) VALUE 0.
77 WK-AVERAGE-GRADE PIC 9(03)V99 VALUE 0.
77 WK-NUMBER-OF-SUBJECTS PIC 9(09) VALUE 0.
77 WK-FORMAT-INDEX PIC ZZZZ9.
77 WK-FORMAT-GRADE PIC ZZ9.99.
01 WK-IF-FOUND PIC 9(01) VALUE 0.
88 WK-FOUND VALUE 1.
88 WK-NOT-FOUND VALUE 0.
01 WK-YES-OR-NO PIC X(01) VALUE' '.
88 WK-YES VALUE'Y'.
88 WK-NO VALUE'N'.
01 WK-MAIN-MENU-OPTIONS PIC X(01) VALUE' '.
88 WK-NEW-SUBJECT VALUE'N'.
88 WK-ALL-SUBJECTS VALUE'A'.
88 WK-SEARCH-SUBJECT VALUE'S'.
88 WK-RETIRE-SUBJECT VALUE'R'.
88 WK-QUIT VALUE'Q'.
01 WK-SUBJECTS OCCURS 1 TO 100 TIMES DEPENDI
- NG ON WK-MAX-SUBJECTS INDEXED BY WK-SUBJECT-INDEX.
02 WK-SUBJECT-NAME PIC X(30).
02 WK-SUBJECT-ID PIC 9(03).
02 WK-SUBJECT-GRADE PIC 9(03)V9(02) VALUE 0.
02 WK-NUMBER-OF-EXAMS PIC 9(01) VALUE 0.
02 WK-EXAMS OCCURS 5 TIMES INDEXED BY WK-
- EXAM-INDEX.
03 WK-EXAM-NAME PIC X(30).
03 WK-EXAM-GRADE PIC 9(03)V9(02) VALUE 0.
PROCEDURE DIVISION.
MAIN-PROCEDURES SECTION.
0001-SYSTEM-ENTRY-POINT.
DISPLAY "SUBJECTS DATA BASE".
PERFORM 0002-MENU UNTIL WK-END-PROGRAM > ZERO.
DISPLAY "GOODBYE!".
STOP RUN.
0002-MENU.
MOVE WK-AVERAGE-GRADE TO WK-FORMAT-GRADE.
DISPLAY "OPTIONS: (N)EW SUBJECT, SEE (A)LL SUBJECTS"
" OR (S)EARCH SUBJECT, (R)ETIRE SUBJECT, (Q)UIT",
DISPLAY "AVERAGE GRADE = " WK-FORMAT-GRADE.
ACCEPT WK-MAIN-MENU-OPTIONS.
IF WK-NEW-SUBJECT
ADD 1 TO WK-LAST-SUBJECT-ADDED
PERFORM 0001-ENTER-SUBJECT UNTIL WK-MAX-SUBJECTS
>= WK-LAST-SUBJECT-ADDED
PERFORM 0005-GENERAL-AVERAGE
ELSE IF WK-ALL-SUBJECTS
PERFORM 0002-SHOW-SUBJECT VARYING WK-SUBJECT-INDEX FROM 1
BY 1 UNTIL WK-SUBJECT-INDEX > WK-MAX-SUBJECTS
ELSE IF WK-SEARCH-SUBJECT
DISPLAY "INTRODUCE THE EXAM'S ID:"
ACCEPT WK-SEARCH-ID
PERFORM 0003-SUBJECT-SEARCH
ELSE IF WK-RETIRE-SUBJECT
PERFORM 0004-RETIRE-SUBJECT
PERFORM 0005-GENERAL-AVERAGE
ELSE IF WK-QUIT
MOVE 1 TO WK-END-PROGRAM
ELSE
DISPLAY "INVALID SELECTION"
PERFORM 0002-MENU.
SUBJECT-PROCEDURES SECTION.
0001-ENTER-SUBJECT.
MOVE 0 TO WK-SEARCH-ID.
PERFORM 0003-SUBJECT-SEARCH.
IF WK-NOT-FOUND AND WK-MAX-SUBJECTS >= 100
DISPLAY "LIMIT OF 100 SUBJECTS REACHED!!"
SUBTRACT 1 FROM WK-LAST-SUBJECT-ADDED
PERFORM 0001-SYSTEM-ENTRY-POINT.
IF WK-FOUND AND WK-SUBJECT-INDEX > WK-MAX-SUBJECTS
ADD 1 TO WK-MAX-SUBJECTS
ELSE IF WK-FOUND
SUBTRACT 1 FROM WK-LAST-SUBJECT-ADDED
ELSE IF WK-NOT-FOUND
ADD 1 TO WK-MAX-SUBJECTS
SET WK-SUBJECT-INDEX TO WK-LAST-SUBJECT-ADDED.
DISPLAY "SUBJECT NAME: "
ACCEPT WK-SUBJECT-NAME(WK-SUBJECT-INDEX).
DISPLAY "NUMBER OF EXAMS: "
ACCEPT WK-NUMBER-OF-EXAMS(WK-SUBJECT-INDEX).
IF WK-NUMBER-OF-EXAMS(WK-SUBJECT-INDEX) <= 5 AND > 0
PERFORM 0001-ENTER-EXAM VARYING WK-EXAM-INDEX FROM 1 BY 1
UNTIL WK-EXAM-INDEX > WK-NUMBER-OF-EXAMS(WK-SUBJECT-INDEX
)
PERFORM 0003-EXAM-AVERAGE
ADD 1 TO WK-ID-COUNTER
MOVE WK-ID-COUNTER TO WK-SUBJECT-ID(WK-SUBJECT-INDEX)
ADD 1 TO WK-NUMBER-OF-SUBJECTS
ELSE
DISPLAY "NUMBER OF EXAMS CAN'T EXCEED 5!!"
IF WK-SUBJECT-INDEX = WK-MAX-SUBJECTS
SUBTRACT 1 FROM WK-MAX-SUBJECTS
END-IF
PERFORM 0001-ENTER-SUBJECT.
0002-SHOW-SUBJECT.
IF WK-SUBJECT-ID(WK-SUBJECT-INDEX) NOT = 0
DISPLAY "SUBJECT NAME:", WK-SUBJECT-NAME(WK-SUBJECT-INDEX
)
MOVE WK-SUBJECT-GRADE(WK-SUBJECT-INDEX)TO WK-FORMAT-GRADE
DISPLAY "SUBJECT GRADE:", WK-FORMAT-GRADE
DISPLAY "ID:", WK-SUBJECT-ID(WK-SUBJECT-INDEX)
DISPLAY "EXAMS:"
PERFORM 0002-SHOW-EXAM VARYING WK-EXAM-INDEX FROM 1 BY 1
UNTIL WK-EXAM-INDEX > WK-NUMBER-OF-EXAMS
- (WK-SUBJECT-INDEX).
0003-SUBJECT-SEARCH.
SET WK-SUBJECT-INDEX TO 1.
SEARCH WK-SUBJECTS
AT END
IF WK-SEARCH-ID NOT = 0
DISPLAY "SUBJECT " WK-SEARCH-ID " NOT FOUND"
END-IF
MOVE 0 TO WK-IF-FOUND
WHEN WK-SUBJECT-ID(WK-SUBJECT-INDEX) = WK-SEARCH-ID
MOVE 1 TO WK-IF-FOUND
PERFORM 0002-SHOW-SUBJECT.
0004-RETIRE-SUBJECT.
DISPLAY "INTRODUCE THE SUBJECT'S ID:".
ACCEPT WK-SEARCH-ID.
PERFORM 0003-SUBJECT-SEARCH.
IF WK-FOUND
DISPLAY "ARE YOU SURE YOU WANT TO RETIRE THIS SUBJECT?",
" (Y)ES, (N)O"
ACCEPT WK-YES-OR-NO
IF WK-YES
IF WK-MAX-SUBJECTS > 0
MOVE LOW-VALUES TO WK-SUBJECTS(WK-SUBJECT-INDEX)
MOVE 0 TO WK-SUBJECT-GRADE(WK-SUBJECT-INDEX)
MOVE 0 TO WK-SUBJECT-ID(WK-SUBJECT-INDEX)
IF WK-LAST-SUBJECT-ADDED = WK-SUBJECT-INDEX
SUBTRACT 1 FROM WK-LAST-SUBJECT-ADDED, WK-MAX
- -SUBJECTS
END-IF
SUBTRACT 1 FROM WK-NUMBER-OF-SUBJECTS
END-IF
ELSE IF WK-NO
NEXT SENTENCE
ELSE
DISPLAY "INVALID SELECTION"
END-IF
ELSE IF WK-NOT-FOUND
DISPLAY "RETURNING TO MAIN MENU".
0005-GENERAL-AVERAGE.
MOVE 0 TO WK-AVERAGE-GRADE
IF WK-NUMBER-OF-SUBJECTS > 0
PERFORM VARYING WK-SUBJECT-INDEX FROM 1 BY 1 UNTIL WK-SUB
- JECT-INDEX > WK-MAX-SUBJECTS
IF WK-SUBJECT-ID(WK-SUBJECT-INDEX) NOT = 0
ADD WK-SUBJECT-GRADE(WK-SUBJECT-INDEX) TO WK-AVERA
- GE-GRADE
END-IF
END-PERFORM
DIVIDE WK-NUMBER-OF-SUBJECTS INTO WK-AVERAGE-GRADE.
EVALUACIONES-PROCEDURE SECTION.
0001-ENTER-EXAM.
MOVE WK-EXAM-INDEX TO WK-FORMAT-INDEX.
DISPLAY "NAME OF EXAM #", WK-FORMAT-INDEX,":".
ACCEPT WK-EXAM-NAME(WK-SUBJECT-INDEX, WK-EXAM-INDEX).
DISPLAY "EXAM GRADE:".
ACCEPT WK-EXAM-GRADE(WK-SUBJECT-INDEX, WK-EXAM-INDEX).
0002-SHOW-EXAM.
MOVE WK-EXAM-GRADE(WK-SUBJECT-INDEX, WK-EXAM-INDEX) TO WK-FOR
- MAT-GRADE.
DISPLAY " ", WK-EXAM-NAME(WK-SUBJECT-INDEX, WK-EXAM-INDEX),
" = ", WK-FORMAT-GRADE.
0003-EXAM-AVERAGE.
IF WK-NUMBER-OF-EXAMS(WK-SUBJECT-INDEX) > 0
PERFORM VARYING WK-EXAM-INDEX FROM 1 BY 1 UNTIL
WK-EXAM-INDEX > WK-NUMBER-OF-EXAMS(WK-SUBJECT-INDEX)
ADD WK-EXAM-GRADE(WK-SUBJECT-INDEX, WK-EXAM-INDEX)
TO WK-SUBJECT-GRADE(WK-SUBJECT-INDEX)
END-PERFORM
DIVIDE WK-NUMBER-OF-EXAMS(WK-SUBJECT-INDEX) INTO WK-SUBJE
- CT-GRADE(WK-SUBJECT-INDEX).
END PROGRAM SUBJECTS-DATA-BASE.