CIS 214 Introduction to COBOL LAB 1 CODE

Objectives:

This is the first real lab. The intent is to get familiar with the MicroFocus workbench and how to enter a program, check (compile) it and animate (run) it. Internet COBOL students will electronically turn in a copy of their COBOL code (with the .CBL extension) and the output code to the course e-mail address.


Although I have used upper case (CAPITAL) letters in the program, MicroFocus COBOL is very forgiving editor (main frame COBOL is not) in terms of letter case. Notice that COBOL uses the hyphen (-) and not the underscore (_).

There are two definite margins - margin (or area) A - which is from columns 8 to 11, and margin (or area) B - which is from columns 12 to 72.

Type this in as shown - possibly not entering the lines that start with an asterisk in column 7 and are in lower case - these are comment lines. Change the company name on Heading-Line-1 and the location on Heading-Line-2 to your company name and your hometown.

COBOL is generally very wordy - and that is one of the reasons it is preferred in the business setting, it tends to be self documenting and thus fairly easy to change and modify. I joke with my campus students and say this class is one credit programming and two credits typing!

Here is the program. This is different than lab 0 as it has more lines in the Working-Storage section - which allows us to have various output lines - heading lines, column-heading lines and detail-lines. This is a more robust program and closer to a real COBOL program.  (You don't have to type in the comment lines - comment lines start with an asterisk in column 7).

(Note.  If you "copy-and-paste" into the Microfocus editor, you may have to adjust the lines as statements such as IDENTIFICATION DIVISION start in column 8.  Sometimes when you paste the statements start over too far and Microfocus does not recognize the statement.)

 

       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.  


Click here to see the output report

Return to Internet COBOL main page

Last modified by Bruce White on 02/02/04