0% found this document useful (0 votes)
41 views

Description: This Program Accepts Two Numbers and Adds Them Together

This program reads records from a sequential input file, counts the total number of records and the number of male and female students, and writes a report to a sequential output file. It loops through the input file, incrementing count variables for total, male, and female records, and writes the results to the output file when it reaches the end of the input file.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Description: This Program Accepts Two Numbers and Adds Them Together

This program reads records from a sequential input file, counts the total number of records and the number of male and female students, and writes a report to a sequential output file. It loops through the input file, incrementing count variables for total, male, and female records, and writes the results to the output file when it reaches the end of the input file.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Description

This program accepts two numbers and adds them together.

Source Code
000100 ID DIVISION.
000200 PROGRAM-ID. ACCEPT1.
000300 DATA DIVISION.
000400 WORKING-STORAGE SECTION.
000500 01 WS-FIRST-NUMBER PIC 9(3).
000600 01 WS-SECOND-NUMBER PIC 9(3).
000700 01 WS-TOTAL PIC ZZZ9.
000800*
000900 PROCEDURE DIVISION.
001000 0000-MAINLINE.
001100 DISPLAY 'ENTER A NUMBER: '.
001200 ACCEPT WS-FIRST-NUMBER.
001300*
001400 DISPLAY 'ANOTHER NUMBER: '.
001500 ACCEPT WS-SECOND-NUMBER.
001600*
001700 COMPUTE WS-TOTAL = WS-FIRST-NUMBER + WS-SECOND-NUMBER.
001800 DISPLAY 'THE TOTAL IS: ', WS-TOTAL.
001900 STOP RUN.

Sample Run
ENTER A NUMBER: 7
ANOTHER NUMBER: 7
THE TOTAL IS: 14

Description
This program takes all input records of salesperson data and writes it to an output file
reformatted.

Source Code

000100 ID DIVISION.
000200 PROGRAM-ID. SLS02.
000300 FILE-CONTROL.
000400 SELECT SALESPERSON-FILE
000500 ASSIGN TO DISK.
000600 SELECT REPORT-FILE
000700 ASSIGN TO PRINTER.
000800 DATA DIVISION.
000900 FILE SECTION.
001000 FD SALESPERSON-FILE.
001100 01 SALESPERSON-RECORD.
001200 05 FILLER PIC XX.
001300 05 SP-NUMBER PIC X(4).
001400 05 SP-NAME PIC X(18).
001500 05 FILLER PIC X(21).
001600 05 SP-CURRENT-SALES PIC 9(5)V99.
001700 05 SP-CURRENT-RETURNS PIC 9(4)V99.
001800 FD REPORT-FILE.
001900 01 REPORT-RECORD.
002000 05 FILLER PIC X(10).
002100 05 RT-NUMBER PIC X(4).
002200 05 FILLER PIC X(6).
002300 05 RT-NAME PIC X(18).
002400 05 FILLER PIC X(6).
002500 05 RT-CURRENT-SALES PIC ZZ,ZZZ.99.
002600 05 FILLER PIC X(6).
002700 05 RT-CURRENT-RETURNS PIC Z,ZZZ.99.
002800 05 FILLER PIC X(65).
002900 WORKING-STORAGE SECTION.
003000 01 WS-EOF-FLAG PIC X.
003100*
003200 PROCEDURE DIVISION.
003300*
003400 MAIN-ROUTINE.
003500 OPEN INPUT SALESPERSON-FILE
003600 OUTPUT REPORT-FILE
003700 MOVE "N" TO WS-EOF-FLAG
003800 READ SALESPERSON-FILE
003900 AT END MOVE "Y" TO WS-EOF-FLAG
004000 END-READ
004100*
004200 PERFORM UNTIL WS-EOF-FLAG IS EQUAL TO "Y"
004300 MOVE SPACES TO REPORT-RECORD
004400 MOVE SP-NUMBER TO RT-NUMBER
004500 MOVE SP-NAME TO RT-NAME
004600 MOVE SP-CURRENT-SALES TO RT-CURRENT-SALES
004700 MOVE SP-CURRENT-RETURNS TO RT-CURRENT-RETURNS
004800 WRITE REPORT-RECORD
004900 READ SALESPERSON-FILE
005000 AT END MOVE "Y" TO WS-EOF-FLAG
005100 END-READ
005200 END-PERFORM
005300*
005400 CLOSE SALESPERSON-FILE, REPORT-FILE
005500 STOP RUN.
Sample Run

0005 BENNETT ROBERT 1,600.35 12.50


0016 LOCK ANDREW S 357.72 79.85
0080 PARKER JAMES E 18,200.00 165.00
0401 REDDING OLIVIA 16,123.99 2,301.75
1375 BENTON ALEX J 3,250.00 56.50
1442 ADAMS JUNE R 4,635.21 125.16
1842 COLE ROBERT N 14,285.14 6,385.29

$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. SeqWrite.
AUTHOR. Michael Coughlan.
* Example program showing how to create a sequential file
* using the ACCEPT and the WRITE verbs.
* Note: In this version of COBOL pressing the Carriage Return (CR)
* without entering any data results in StudentDetails being filled
* with spaces.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT StudentFile ASSIGN TO "STUDENTS.DAT"
ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 DateOfBirth.
03 YOBirth PIC 9(4).
03 MOBirth PIC 9(2).
03 DOBirth PIC 9(2).
02 CourseCode PIC X(4).
02 Gender PIC X.

PROCEDURE DIVISION.
Begin.
OPEN OUTPUT StudentFile
DISPLAY "Enter student details using template below. Enter no data to
end."

PERFORM GetStudentDetails
PERFORM UNTIL StudentDetails = SPACES
WRITE StudentDetails
PERFORM GetStudentDetails
END-PERFORM
CLOSE StudentFile
STOP RUN.

GetStudentDetails.
DISPLAY "Enter - StudId, Surname, Initials, YOB, MOB, DOB, Course, Gender"
DISPLAY "NNNNNNNSSSSSSSSIIYYYYMMDDCCCCG"
ACCEPT StudentDetails.

Input file to read data

$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. SeqReadNo88.
AUTHOR. Michael Coughlan.
* An example showing how to read a sequential file without
* using condition names.
* See SeqRead.CBL for the definitive example.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT StudentFile ASSIGN TO "STUDENTS.DAT"
ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 DateOfBirth.
03 YOBirth PIC 9(4).
03 MOBirth PIC 9(2).
03 DOBirth PIC 9(2).
02 CourseCode PIC X(4).
02 Gender PIC X.

PROCEDURE DIVISION.
Begin.
OPEN INPUT StudentFile
READ StudentFile
AT END MOVE HIGH-VALUES TO StudentDetails
END-READ
PERFORM UNTIL StudentDetails = HIGH-VALUES
DISPLAY StudentId SPACE StudentName SPACE CourseCode SPACE YOBirth
READ StudentFile
AT END MOVE HIGH-VALUES TO StudentDetails
END-READ
END-PERFORM
CLOSE StudentFile
STOP RUN.

Seq file with condition level 88

$ SET SOURCEFORMAT "FREE"


IDENTIFICATION DIVISION.
PROGRAM-ID. InsertRecords.
AUTHOR. Michael Coughlan.
* This program updates the Students.Dat file with insertions
* taken from the Transins.Dat file to create a new file
* - Students.New - which contains the inserted records.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT StudentRecords ASSIGN "STUDENTS.DAT"
ORGANIZATION IS LINE SEQUENTIAL
ACCESS MODE IS SEQUENTIAL.

SELECT TransRecords ASSIGN "TRANSINS.DAT"


ORGANIZATION IS LINE SEQUENTIAL
ACCESS MODE IS SEQUENTIAL.

SELECT NewStudentRecords ASSIGN "STUDENTS.NEW"


ORGANIZATION IS LINE SEQUENTIAL
ACCESS MODE IS SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD StudentRecords.
01 StudentRecord.
88 EndOfStudentFile VALUE HIGH-VALUES.
02 StudentID PIC X(7).
02 FILLER PIC X(23).

FD TransRecords.
01 TransRecord.
88 EndOfTransFile VALUE HIGH-VALUES.
02 TransStudentID PIC X(7).
02 FILLER PIC X(23).

FD NewStudentRecords.
01 NewStudentRecord PIC X(30).
PROCEDURE DIVISION.
BEGIN.
OPEN INPUT StudentRecords
OPEN INPUT TransRecords
OPEN OUTPUT NewStudentRecords

READ StudentRecords
AT END SET EndOfStudentFile TO TRUE
END-READ

READ TransRecords
AT END SET EndOfTransFile TO TRUE
END-READ

PERFORM UNTIL (EndOfStudentFile) AND (EndOfTransFile)


EVALUATE TRUE
WHEN (StudentID < TransStudentID)
WRITE NewStudentRecord FROM StudentRecord
READ StudentRecords
AT END SET EndOfStudentFile TO TRUE
END-READ

WHEN (StudentID > TransStudentID)


WRITE NewStudentRecord FROM TransRecord
READ TransRecords
AT END SET EndOfTransFile TO TRUE
END-READ

WHEN (StudentID = TransStudentID)


DISPLAY "Error - " TransStudentId " already exists in file"
READ TransRecords
AT END SET EndOfTransFile TO TRUE
END-READ
END-EVALUATE
END-PERFORM

CLOSE StudentRecords
CLOSE TransRecords
CLOSE NewStudentRecords
STOP RUN.

Seq file with report writing

$ SET SOURCEFORMAT"FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID. StudentNumbersReport .
AUTHOR. Michael Coughlan.

*INPUT The student record file Students.Dat Records in this file


* are sequenced on ascending Student Number.
*OUTPUT Shows the number of student records in the file and the
* number of records for males and females.
*PROCESSING For each record read;
* Adds one to the TotalStudents count
* IF the Gender is Male adds one to TotalMales
* IF the Gender is Female adds one to TotalFemales
* At end of file writes the results to the report file.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT StudentFile ASSIGN TO "STUDENTS.DAT"
ORGANIZATION IS LINE SEQUENTIAL.
SELECT ReportFile ASSIGN TO "STUDENTS.RPT"
ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
88 EndOfStudentFile VALUE HIGH-VALUES.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 DateOfBirth.
03 YOBirth PIC 9(4).
03 MOBirth PIC 9(2).
03 DOBirth PIC 9(2).
02 CourseCode PIC X(4).
02 Gender PIC X.
88 Male VALUE "M", "m".

FD ReportFile.
01 PrintLine PIC X(40).

WORKING-STORAGE SECTION.
01 HeadingLine PIC X(21) VALUE " Record Count Report".

01 StudentTotalLine.
02 FILLER PIC X(17) VALUE "Total Students = ".
02 PrnStudentCount PIC Z,ZZ9.

01 MaleTotalLine.
02 FILLER PIC X(17) VALUE "Total Males = ".
02 PrnMaleCount PIC Z,ZZ9.

01 FemaleTotalLine.
02 FILLER PIC X(17) VALUE "Total Females = ".
02 PrnFemaleCount PIC Z,ZZ9.
01 WorkTotals.
02 StudentCount PIC 9(4) VALUE ZERO.
02 MaleCount PIC 9(4) VALUE ZERO.
02 FemaleCount PIC 9(4) VALUE ZERO.

PROCEDURE DIVISION.
Begin.
OPEN INPUT StudentFile
OPEN OUTPUT ReportFile

READ StudentFile
AT END SET EndOfStudentFile TO TRUE
END-READ
PERFORM UNTIL EndOfStudentFile
ADD 1 TO StudentCount
IF Male ADD 1 TO MaleCount
ELSE ADD 1 TO FemaleCount
END-IF
READ StudentFile
AT END SET EndOfStudentFile TO TRUE
END-READ
END-PERFORM

PERFORM PrintReportLines

CLOSE StudentFile, ReportFile


STOP RUN.

PrintReportLines.
MOVE StudentCount TO PrnStudentCount
MOVE MaleCount TO PrnMaleCount
MOVE FemaleCount TO PrnFemaleCount

WRITE PrintLine FROM HeadingLine


AFTER ADVANCING PAGE
WRITE PrintLine FROM StudentTotalLine
AFTER ADVANCING 2 LINES
WRITE PrintLine FROM MaleTotalLine
AFTER ADVANCING 2 LINES
WRITE PrintLine FROM FemaleTotalLine
AFTER ADVANCING 2 LINES.

You might also like