Objectives:
first required COBOL program
develop proficiency with MicroFocus environment
understand the basic structures of COBOL - the four divisions, the data structures, the structure and logic of the procedure division.
IDENTIFICATION DIVISION.
PROGRAM-ID. COBOL-LAB1.
AUTHOR. BRUCE A. WHITE.
******************************************************************
ENVIRONMENT DIVISION.
* defines the external files - an input file and output file
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PAYROLL-FILE ASSIGN TO "A:\lab1.txt"
ORGANIZATION IS LINE SEQUENTIAL.
SELECT REPORT-FILE ASSIGN TO "A:\LAB1out.txt"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
* has two sections - the file section that describes the files
* and the working storage section - where output lines and
* processing variables are defined
FILE SECTION.
FD PAYROLL-FILE.
* this defines the structure of the input-file
01 PAYROLL-RECORD.
05 FIRST-NAME-IN PIC X(12).
05 LAST-NAME-IN PIC X(15).
05 HOURS-WORKED-IN PIC 99V9.
05 RATE-OF-PAY-IN PIC 99V99.
*****************************************************************
* (note that comments MUST have an asterisk in column 7)
* the payroll file has four fields: first name, last name,
* hours worked and the rate of pay
*
* first name is up to 12 characters long (thus a picture
* clause of X(12) indicating 12 characters
*
* the last name is up to 15 characters long (and if not that
* long it has spaces)
*
* the hours-worked field is three numeric digits with an
* implied decimal place - so 40 hours would be 400 - and COBOL
* will treat this as 40.0
*
* the rate-of-pay field is four numeric digits long with two
* implied decimal places - so $12.75 would be 1275 in the data
* file
*****************************************************************
FD REPORT-FILE.
01 REPORT-RECORD PIC X(80).
* this defines an output file (in this case 80 bytes long)
WORKING-STORAGE SECTION.
* defines processing variables and output lines
01 FLAGS-AND-ACCUMULATORS.
05 END-OF-FILE PIC XXX VALUE "NO".
05 GROSS-PAY PIC 9999V99 VALUE ZERO.
01 HEADING-LINE-1.
05 PIC X(29) VALUE SPACES.
05 PIC X(30) VALUE
"WHITE SALES ENTERPRISES".
* (change the heading to be your name/company)
01 HEADING-LINE-2.
05 PIC X(30) VALUE SPACES.
05 PIC X(21) VALUE
"MADISON, SOUTH DAKOTA".
01 COLUMN-HEADING.
05 PIC X(03) VALUE SPACES.
05 PIC X(14) VALUE "FIRST NAME".
05 PIC X(20) VALUE "LAST NAME".
05 PIC X(10) VALUE "HOURS".
05 PIC X(10) VALUE "RATE".
05 PIC X(15) VALUE "GROSS PAY".
01 DETAIL-LINE.
05 PIC X(03) VALUE SPACES.
05 FIRST-NAME-OUT PIC X(14).
05 LAST-NAME-OUT PIC X(18).
05 PIC X(03) VALUE SPACES.
05 HOURS-WORKED-OUT PIC Z9.9.
05 PIC X(04) VALUE SPACES.
05 RATE-OF-PAY-OUT PIC Z9.99.
05 PIC X(07) VALUE SPACES.
05 GROSS-PAY-OUT PIC Z,ZZ9.99.
*****************************************************************
* DETAIL-LINE comments.
* on the detail line we are writing out the payroll information
* in particular, when we write out the hours-worked, rate-of-pay
* and the gross-pay, we use actual decimal points and also we
* will zero suppress any leading zeros. (and with gross-pay-out
* we are printing a comma if the value is over 1000).
*
* We need to use different variable names not to confuse COBOL,
* so names like FIRST-NAME-OUT is used here and FIRST-NAME-IN
* is used in the data file and we will move first-name-in to
* first-name-out when we process the data
*****************************************************************
PROCEDURE DIVISION.
* finally - where the real work gets done
* it is divided into paragraphs (or modules) generally called
* from the main controlling module (here 1000-MAIN-CONTROL).
*
* note - we will use this structure for most of our programs!
* for lab2 you may want to open this and then "save as" lab2.cbl
* I normally have 1000-main be the control module, 2000-initialize
* 3000-Process and 4000-Terminate - although what is in those modules
* may vary
1000-MAIN-CONTROL.
PERFORM 2000-INITIALIZE.
PERFORM UNTIL END-OF-FILE = "YES"
READ PAYROLL-FILE
AT END
MOVE "YES" TO END-OF-FILE
NOT AT END
PERFORM 3000-PROCESS
END-PERFORM
PERFORM 4000-TERMINATE.
STOP RUN.
2000-INITIALIZE.
OPEN INPUT PAYROLL-FILE
OUTPUT REPORT-FILE.
WRITE REPORT-RECORD FROM HEADING-LINE-1.
WRITE REPORT-RECORD FROM HEADING-LINE-2.
WRITE REPORT-RECORD FROM SPACES.
WRITE REPORT-RECORD FROM COLUMN-HEADING.
WRITE REPORT-RECORD FROM SPACES.
3000-PROCESS.
* first calculate the gross pay
MULTIPLY HOURS-WORKED-IN BY RATE-OF-PAY-IN
GIVING GROSS-PAY.
* now we have to move both the input data and calculated data
* to set up the detail line.
MOVE FIRST-NAME-IN TO FIRST-NAME-OUT.
MOVE LAST-NAME-IN TO LAST-NAME-OUT.
MOVE HOURS-WORKED-IN TO HOURS-WORKED-OUT.
MOVE RATE-OF-PAY-IN TO RATE-OF-PAY-OUT.
MOVE GROSS-PAY TO GROSS-PAY-OUT.
WRITE REPORT-RECORD FROM DETAIL-LINE.
4000-TERMINATE.
CLOSE PAYROLL-FILE, REPORT-FILE.
![]()
From this example we can see:
COBOL has four units called "DIVISIONS"
The first division is the IDENTIFICATION DIVISION. It has one required statement PROGRAM-ID and identifies the program. It may also have other statements such as AUTHOR and comments stating what the program does.
The second division is the ENVIRONMENT DIVISION. It's purpose is to interface the COBOL program statements with an operating system, and in particular to datafiles. One of the great strengths of COBOL is in batch processing and making of reports for management. Most of our labs will do that - read an input file and create an output report.
The third division is the DATA DIVISION. It has two sections: the FILE SECTION (which describes the data files that were first mentioned in the Environment Division) and the WORKING-STORAGE SECTION (which describes all output structures and processing variables. The DATA DIVISION can become quite long. It will have data items with PICTURE (or PIC) clauses that describes the data. A PIC of X(5) indicates a five byte long character field, while a PIC of 999V99 indicates a numeric field with two decimal places that could be as large as 999.99.
The final division is the PROCEDURE DIVISION. Here is where the
processing takes place. Most COBOL programmers divide the PROCEDURE DIVISION into modules.
Generally I use the 1000 module as the controlling module or main module, the 2000 module
as a pre-processing or initialization module, the 3000 module as the processing module and
depending on the application other modules as needed. The textbook authors use modules of
100, 200, etc., while I prefer the numbering in the 1000 range. In real applications this
can become very long as well. Most real COBOL programs are over 2000 lines long - and some
are a lot longer than that!
After writing the program, you will need to "check" it. If it checks clean, then you can execute it; if it doesn't check clean, you will need to correct the syntax errors.
You might need to re-open the file in execution mode (files can be opened for edit or they can be opened for execution). Execution files will have the file extension of .int. You then will run the program. You should get this message:
| If you have difficulties or want some
additional help in getting this accomplished, PLEASE e-mail me
or call me (203) 281-6482 (home) or (203) 582-3386 (office). I'll call you and we'll walk our way
through the editor, through the processes and the application!!!!
Bruce |
Click here to see the data file.
Return to Internet COBOL main page