COBOL From Micro To Mainframe 3rd Edition PDF
COBOL From Micro To Mainframe 3rd Edition PDF
RT:elEntT T. [3Ft.AUEnl
AR{f L VnzcpuEz VtLL,A,Fl
ATTTHUR R. EIUgs
THIRD EDITIOil
ISBN O-I3_?1DBI7-A
lffifiilflillilililllililtlflll
Chapter 1 - Introduction
re 1.6 (continued)
23 FD PRINT-FILE
24 RECORD CONTAINS 132 CHARACTERS
25 DATA RECORD IS PRINT-LINE.
26 01 PRINT-LINE PIC X(132).
27
28 WORKING-STORAGE SECTION.
29 01 DATA-REMAINS-SWITCH PIC X(2) VALUE SPACES.
30
31 01 HEADING-LINE.
~~~Da(a Division
32 05 FILLER PIC X(10) VALUE SPACES.
33 05 FILLER PIC X(12) VALUE 'STUDENT NAME'
34 05 FILLER PIC X(110) VALUE SPACES.
36 01 DETAIL-LINE.
37 05 FILLER PIC X(8) VALUE SPACES.
38 05 PRINT-NAME PIC 1(25).
39 05 FILLER PIC X(99) VALUE SPACES. I
40
41 PROCEDURE DIVISION.
42 PREPARE-SENIOR-REPORT.
43 OPEN INPUT STUDENT-FILE
44 OUTPUT PRINT-FILE.
45 READ STUDENT-FILE
46 AT END MOVE 'NO' TO DATA-REMAINS-SWITCH
47 END-READ.
48 PERFORM WRITE-HEADING-LINE.
49 PERFORM PROCESS-RECORDS
50 UNTIL DATA-REMAINS-SWITCH = 'NO'.
51 CLOSE STUDENT-FILE
52 PRINT-FILE.
53 STOP RUN.
54
55 WRITE-HEADING-LINE. Procedure Division
56 MOVE HEADING-LINE TO PRINT-LINE.
57 WRITE PRINT-LINE.
58
59 PROCESS-RECORDS.
60 IF STU-CREDITS > 110 AND STU-MAJOR = 'ENGINEERING'
61 MOVE STU-NAME TO PRINT-NAME
62 MOVE DETAIL-LINE TO PRINT-LINE
63 WRITE PRINT-LINE
64 END-IF.
65 READ STUDENT-FILE
66 AT END MOVE 'NO' TO DATA-REMAINS-SWITCH
67 END-READ.
Flowchart Blocks for Problem 7
IDENTIFICATION DIVISION.
PROGRAM-ID. FIRSTTRY.
GRAUER.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT EMPLOYEE-FILE ASSIGN TO 'A:\CHAPTR02\FIRSTTRY.DAT'
ORGANIZATION IS LINE SEQUENTIAL.
PRINT-FILE
ASSIGN TO PRINTER.
FILE SECTION.
FD EMPLOYEE-FILE
RECORD CONTAINS 44 CHARACTERS
DATA RECORD IS EMPLOYEE-RECORD.
01 EMPLOYEE-RECORD.
05 EMP-NAME PIC X(25).
05 EMP-TITLE PIC X(10).
05 EMP-AGE PIC 99.
05 FILLER PIC XX.
05 EMP-SALARY PIC 9(5).
Chapter 1 — Introduction
25 FD 4
a n d t h u s r e d u c i n g t h e n u m b e r of t i m e s t h e i n p u t / o u t p u t d e v i c e is a c c e s s e d . In
o t h e r w o r d s , it is m o r e efficient t o a c c e s s a disk o n c e a n d r e a d a b l o c k c o n t a i n i n g 10
r e c o r d s , t h a n it is t o a c c e s s t h e disk 10 t i m e s a n d r e a d e a c h r e c o r d individually. T h e
blocking factor is d e f i n e d as t h e n u m b e r o f logical records in a physical record. T h e
c o n c e p t is illustrated i n Figure 4.2 w h e r e t h e r e c o r d s o f F i g u r e 4 . 2 a are u n b l o c k e d ,
w h e r e a s t h o s e i n F i g u r e s 4 . 2 b a n d 4 . 2 c h a v e b l o c k i n g factors o f 2 a n d 3, r e s p e c t i v e l y .
T h e h i g h e r t h e b l o c k i n g factor, t h e f e w e r t h e n u m b e r o f p h y s i c a l r e c o r d s , a n d
t h e m o r e efficient t h e p r o c e s s i n g . T h u s , t h e b l o c k i n g factor s h o u l d a l w a y s b e a s
h i g h as p o s s i b l e , w i t h i n the l i m i t a t i o n s of the p h y s i c a l d e v i c e . T h e actual
d e t e r m i n a t i o n o f t h e b l o c k i n g factor n e e d n o t c o n c e r n u s n o w ; w h a t is i m p o r t a n t is
t h e i m p l e m e n t a t i o n o f b l o c k i n g i n a COBOL p r o g r a m .
A s s u m e , for e x a m p l e , a b l o c k i n g factor o f 5, w i t h t h e a s s o c i a t e d entry, BLOCK
C O N T A I N S 5 RECORDS. T h e initial e x e c u t i o n o f t h e READ s t a t e m e n t p l a c e s a b l o c k
o f 5 logical r e c o r d s i n m e m o r y , w i t h o n l y t h e first r e c o r d a v a i l a b l e t o t h e p r o g r a m .
T h e s e c o n d (third, fourth, a n d fifth) e x e c u t i o n o f t h e READ s t a t e m e n t m a k e s a n e w
logical record available, without a c o r r e s p o n d i n g physical o p e r a t i o n taking place.
In s i m i l a r f a s h i o n t h e sixth e x e c u t i o n o f t h e READ s t a t e m e n t will b r i n g a n e w
p h y s i c a l r e c o r d i n t o t h e I / O area, w i t h n e w l o g i c a l r e c o r d s m a d e available o n t h e
s e v e n t h t h r o u g h t e n t h e x e c u t i o n s o f t h e READ s t a t e m e n t . All o f t h i s is a u t o m a t i c a l l y
d o n e for t h e p r o g r a m m e r as l o n g a s t h e BLOCK C O N T A I N S s t a t e m e n t is s p e c i f i e d in
t h e COBOL F D .
C h a p t e r 5 — The Procedure Division
A B
B e f o r e e x e c u t i o n : [15] 100
A f t e r e x e c u t i o n : 10
MULTIPLY
[identi f i e r - 1 ! r ,- v,
MULTIPLY { \ BY {identifier-2 ROUNDEDU . . .
1 J
[literal-1 J - l J
[ON S I Z E E R R O R imperative-statement-l]
[END-MULTIPLY]
[identifier- l| [identifier-2|
MULTIPLY { } BY { }
[ON S I Z E E R R O R imperative-statement-l]
[END-MULTIPLY]
A B
B e f o r e e x e c u t i o n :
C h a p t e r 5 The Procedure Divisio
P R O G R A M M I N C T I P
The COMPUTE statement should always be used when multiple arithmetic operators are involved.
Consider two sets of equivalent code:
•B + 4¥~-4AC
It is fairly easy to determine what is happening from the single COMPUTE statement, but next to
impossible to realize the cumulative effect of the eight arithmetic statements. Interpretation of the unacceptable
code is further clouded by the mandatory definition of data names for intermediate results, RESULT-1,
RESULT-2, etc.
Parentheses are often required in COMPUTE statements to alter the normal hierarchy of operations; for
example, parentheses are required around 2 * A in the denominator. If they had been omitted, the numerator
would have been divided by 2 and then the quotient would have been multiplied by A. Sometimes the
parentheses are optional to the compiler but should be used to clarify things for the programmer. The
parentheses around 4 * A * C do not alter the normal order of operations and hence are optional.
Individual arithmetic statements are preferable to the COMPUTE statement when only a single operation
is required. Hence, ADD 1 TO COUNTER is easier to read than COMPUTE COUNTER = COUNTER + 1.
unaffected. E x a m p l e 5.9 parallels 5.8 except that B Y replaces I N T O , resulting in a
quotient of zero a n d a remainder of 10. Table 5.10 contains additional examples of
the D I V I D E statement.
Example 5.7 DIVIDE A INTO B.
10| [501
I5I
|10| |51| } 13 j
|10! [51I 5 I
,10, \Sl\
93
94 01 DASH-LINE.
95 05 FILLER PIC X(31)VALUE SPACES.
96 05 FILLER PIC X(8) VALUE ALL
97 05 FILLER PIC X(2) VALUE SPACES.
98 05 FILLER PIC X(8) VALUE ALL
99 05 FILLER PIC X(2) VALUE SPACES.
100 05 FILLER PIC X(7) VALUE ALL '-'.
101 05 FILLER PIC X(6) VALUE SPACES.
102 05 FILLER PIC X(7) VALUE ALL
103 05 FILLER PIC X(5) VALUE SPACES.
104 05 FILLER PIC X(7) VALUE ALL '-'.
105 05 FILLER PIC X(49) VALUE SPACES.
106
107 01 TOTAL-LINE.
108 05 FILLER PIC X(8) VALUE SPACES.
109 05 FILLER PIC X(17)
110 VALUE 'UNIVERSITY TOTALS'.
ntf' y /Q\
111 05 FILLER Tit A\Oj VALUE SPACES.
112 05 TOT-TUITION PIC 9(6).
113 05 FILLER PIC X(6) VALUE SPACES.
114 05 TOT-UNION-FEE PIC 9(4).
115 05 riLLcn PIC X(5) VALUE SPACES.
116 05 TOT-ACTIVITY-FEE PIC 9(4).
117 05 FILLER PIC X(7) VALUE SPACES.
118 05 TOT-SCHOLARSHIP PIC 9(6).
119 05 FILLER PIC X(6) VALUE SPACES.
120 05 TOT-IND-BILL PIC 9(6).
121 05 FILLER PIC X(49) VALUE SPACES.
122
123 PROCEDURE DIVISION. Reserved •;•>vd use f
- J i
124 START. " "
125 OPEN INPUT STUDENT-FILE
126 OUTPUT PRINT-FILE.
127 PERFORM WRITE-HEADING-LINE.
128 PERFORM READ-STUDENT-FILE.
129 PERFORM PROCESS-STUDENT-RECORD
130 UNTIL DATA-REMAINS-SWITCH = 'NO'.
131 PERFORM WRITE-UNIVERSITY-TOTALS.
132 CLOSE STUDENT-FILE
133 PRINT-FILE.
n
i i
STOP fti
m i
135
136 WRITE-HEADING-LINE.
137 MOVE HEADING-LINE TO PRINT -LINE.
138 WRITE PRINT-LINE
139 AFTER ADVANCING PAGE.
140 MOVE SPACES TO PRINT-LINE.
141 WRITE PRINT-LINE.
142
i
C h a p t e r 6— Debugging
(continued)
11 ASSIGN TO PRINTER.
12
13 DATA DIVISION.
14 FILE SECTION.
15 FD STUDENT-FILE
16 RECORD CONTAINS 27 CHARACTERS.
17 01 STUDENT-RECORD.
18 05 STU-NAME.
19 10 STU-LAST-NAME PIC X(15)
20 10 STU-INITIALS PIC XX.
21 05 STU-CREDITS PIC 9(2).
22 05 STU-UNION-MEMBER PIC X.
23 05 STU-SCHOLARSHIP PIC 9(4).
24 05 STU-GPA - - — _ _
25
26 FD PRINT-FILE
27 RECORD CONTAINS 132 CHARACTERS.
28 01 PRINT-LINE PIC X(132).
on
30 WORKING-STORAGE SECTION.
31 01 DATA-REMAINS-SWITCH PIC X(2) VALUE SPACES
32
33 01 INDIVIDUAL-CALCULATIONS.
34 05 IND-TUITION PIC 9(4) VALUE ZEROS.
35 05 IND-ACTIVITY-FEE PIC 9(2) VALUE ZEROS.
36 05 IND-UNION-FEE PIC 9(2) VALUE ZEROS.
37 05 IND-SCHOLARSHIP PIC 9(4) VALUE ZEROS.
38 05 IND-BILL PIC 9(6) VALUE ZEROS.
39
40 01 UNIVERSITY-TOTALS.
41 05 UNI-TUITION PIC 9(6) VALUE ZEROS.
42 05 UNI-UNION-FEE PIC 9(4) VALUE ZEROS.
43 05 UNI-ACTIVITY-FEE PIC 9(4) VALUE ZEROS.
44 05 UNI-SCHOLARSHIP PIC 9(6) VALUE ZEROS.
45 05 UNI-IND-BILL PIC 9(6) VALUE ZEROS.
46
47 01 CONSTANTS-AND-RATES.
48 05 PRICE-PER-CREDIT PIC 9(3) VALUE 200.
49 05 UNION-FEE PIC 9(2) VALUE 25.
50 05 ACTIVITY-FEES.
51 10 1ST-ACTIVITY-FEE PIC 99 VALUE 25.
52 10 IST-CREDIT-LIMIT PIC 99 VALUE 6.
53 10 2ND-ACTIVITY-FEE PIC 99 VALUE 50.
54 10 2ND-CREDIT-LIMIT PIC 99 VALUE 12.
55 10 3RD-ACTIVITY-FEE PIC 99 VALUE 75.
C h a p t e r 6 — Debugging
lie
After c o r r e c t i n g t h e file n a m e , w e r e c o m p i l e d t h e p r o g r a m a n d r a n it a g a i n , this
t i m e w i t h t h e r e s u l t s i n Figure 6.5. F i g u r e 6.5a s h o w s a n o t h e r c o m m o n RTS error,
"Illegal Character i n N u m e r i c Field." This error a l m o s t a l w a y s c o m e s f r o m h a v i n g
s p a c e s i n a n u m e r i c field. O n e c a u s e o f t h o s e s p a c e s i s w h e n t h e p r o g r a m r e a d s a
d a t a file that a c t u a l l y h a s s p a c e s i n t h e field. I n o t h e r w o r d s , if t h e field h a s a
PICTURE o f 9(5) a n d t h e actual c o n t e n t s are " 123", t h e p r o g r a m will fail if it tries t o
u s e this field i n a c o m p u t a t i o n . T h i s p r o b l e m i s a d a t a p r o b l e m rather t h a n a
p r o g r a m p r o b l e m . W h e n y o u c r e a t e test data, b e s u r e t o t y p e i n l e a d i n g z e r o s for
a n y n u m e r i c fields. I n t h i s c a s e t h e field s h o u l d h a v e b e e n "00123".
A s e c o n d r e a s o n for s p a c e s i n a n u m e r i c field is w h e n t h e p r o g r a m a t t e m p t s t o
r e a d b e y o n d t h e e n d o f a file. T h e i n c o r r e c t p l a c e m e n t o f t h e READ i n l i n e 149 o f
F i g u r e 6 . 3 c a u s e s this c o n d i t i o n . Figure 6 . 5 b s h o w s t h e l i n e w h e r e t h e error w a s
detected. W e clicked o n e a c h data n a m e in t h e statement, a n d t h e Animator
s h o w e d u s t h e c u r r e n t c o n t e n t s o f t h e field. S T U - C R E D I T S c o n t a i n s o n l y s p a c e s
a n d c a u s e d t h e error. I n this c a s e , w h e n t h e p r o g r a m r e a d b e y o n d t h e e n d o f t h e
file, COBOL i n s e r t e d s p a c e s i n t o S T U D E N T - R E C O R D (line 17), i n c l u d i n g S T U -
CREDITS. T o c o r r e c t t h e p r o b l e m w e r e s t o r e d t h e READ t o t h e e n d o f PROCESS-
S T U D E N T - R E C O R D , r e c o m p i l e d t h e p r o g r a m , a n d reran it. T h i s a c t i o n c o r r e c t s
t h e r u n - t i m e errors, b u t t h e r e are still l o g i c errors t o d e a l w i t h .
) OK j
— — 2
162 COHJ
U' II . rt>i f ! U N .
ffi3
>(,<
ICS COHP-JIL UNION F i t .
1S>6 Si' STl) UNI ON- MF
16? HO BE Z E R O 10
1&« E L SE
16} M OU f UNION-F
m EHD-IF.
J72 court) r* ficnuitv-pe j|A»piyi A(Ml*l!tii Manllorj !i:jst!
l?« mi-H SIU-CREDITS
EUALUAIE TRU E <-- 1 S T CREDIT-LIMIT J
rtOUi: iST-BCTIUIIi-FEE TO IND OCTIUI
!V4 WH
EN STU-CREDITS > 1ST CRKDIT I.IfllT
iVT.
i n
C h a p t e r 6 Debugging
3. Do you agree with the authors' suggestions for successful walkthroughs? Are there
any guidelines you wish to add to the list? To remove from the iist?
4. Identify the syntactical errors in the COBOL fragment in Figure 6.8.
5. Identify the logical errors in the COBOL fragment in Figure 6.9. (Assume there are
no other READ statements in the program.)
6. The COBOL fragment in Figure 6.10a is taken from a program that compiled
cleanly but failed to execute. The error message is in Figure 6.10b. Explain
the problem.
IDENTIFICATION DIVISION.
PROGRAM ID. ERRORS.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
SELECT EMPLOYEE-FILE ASSIGN TO 'A:\CHAPTR06\EMP.DAT'
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD EMPLOYEE-FILE
RECORD CONTAINS 50 CHARACTERS
DATA RECORD IS EMPLOYEE-RECORD.
EMPLOYEE-RECORD.
05 EMP-NAME PIC X(20).
05 EMP-NUMBER PIC X(9).
05 FILLER PIC X(20).
WORKING STORAGE SECTION.
10 END-OF-FILE-SWITCH PIC X(3) VALUE BLANKS.
WORKING-STORAGE SECTION.
01 END-OF-FILE-SWITCH PIC X(3) VALUE 'YES'.
PROCEDURE DIVISION.
MAINLINE.
PERFORM PROCESS-RECORDS
UNTIL END-OF-FILE-SWITCH = 'YES'
PROCESS-RECORDS.
READ EMPLOYEE-FILE
AT END MOVE 'YES' TO END-OF-FILE-SWITCH
END-READ.
Chapter 7— Editing and Coding Standards
P R O G R A M M I N G T I P
Scope terminators are one of the most powerful enhancements in COBOL-85, and in the opinion of the
authors, justify in and of themselves, conversion to the new standard. In its simplest role a scope terminator is
used in place of a period to end a conditional statement—for example, END-IF to terminate an IF statement.
(A scope terminator and a period should not appear together unless the period also ends the sentence.)
One of the most important reasons for using scope terminators is that they eliminate the very subtle
column 73problem which has always existed, and which is depicted below. The intended logic is straightforward,
and is supposed to apply a discount of two percent on an order of $2,000 or more. The amount due (NET) is
equal to the amount ordered less the discount (if any).
(continued)
HEADING-LINE.
05 FILLER PIC X VALUE SPACES.
05 FILLER PIC X(12) VALUE 'STUDENT NAME'
05 FILLER PIC X(10) VALUE SPACES.
05 FILLER PIC X(7) VALUE 'CREDITS'.
05 FILLER PIC X(2) VALUE SPACES.
05 FILLER PIC X(7) VALUE 'TUITION' .
05 FILLER PIC X(2) VALUE SPACES.
05 FILLER PIC X(9) VALUE 'UNION FEE'.
05 FILLER PIC X(2) VALUE SPACES.
05 FILLER PIC X(7) VALUE 'ACT FEE'.
05 FILLER PIC X{2) VALUE SPACES.
05 FILLER PIC X(ll) VALUE 'SCHOLARSHIP'.
05 FILLER PIC X(2) VALUE SPACES.
05 FILLER PIC X(10) VALUE 'TOTAL BILL'.
05 FILLER PIC X(48) VALUE SPACES.
01 DETAIL-LINE.
05 FILLER PIC X VALUE SPACES.
05 DET-LAST-NAME PIC X(15).
05 FILLER PIC X(2) VALUE SPACES.
05 DET-INITIALS PIC X(2).
05 FILLER PIC X(5) VALUE SPACES.
05 DET-CREDITS PIC Z9.
05 FILLER PIC X(4) VALUE SPACES.
05 DET-TUITION PIC $$$$,$$9.
05 FILLER PIC X(6) VALUE SPACES.
05 DET-UNION-FEE PIC $$$9 BLANK WHEN ZERO
05 FILLER PIC X(5) VALUE SPACES.
05 DET-ACTIVITY-FEE PIC $$$9 BLANK WHEN ZERO
05 FILLER PIC X(6) VALUE SPACES.
05 DET-SCHOLARSHIP PIC $$,$$$9 BLANK WHEN ZERO
05 FILLER PIC X(4) VALUE SPACES.
05 DET-IND-BILL PIC $$$$,$$9CR.
05 r-r ri L tL rQ rjK
I
PIC X(47) VALUE SPACES.
DASH-LINE.
05 FILLER PIC X(31) VALUE SPACES.
05 FILLER PIC X(8) VALUE ALL '-'.
05 FILLER PIC X(2) VALUE SPACES.
05 FILLER PIC X(8) VALUE ALL
05 FILLER PIC X(2) VALUE SPACES.
05 FILLER PIC X(7) VALUE ALL '-'.
C h a p t e r 8 Data Validation
true or false. Four types of conditions are possible: relational, class, sign, a n d
condition-name, each of which is discussed m a separate section.
CREATE
VALID FILE
1 IDENTIFICATION DIVISION.
2 PROGRAM-ID. VALCARS8.
3 AUTHOR. CVV.
4
5 ENVIRONMENT DIVISION.
6 INPUT-OUTPUT SECTION.
7 FILE-CONTROL.
8 SELECT RENTAL-FILE ASSIGN TO 'A:\CHAPTR08\VALCARS.DAT'
9 ORGANIZATION IS LINE SEQUENTIAL.
10 SELECT VALID-RENTAL-FILE ASSIGN TO 'A:\CHAPTR08\VALRENT.DAT
11 ORGANIZATION IS LINE SEQUENTIAL.
12 SELECT ERROR-FILE
13 ASSIGN TO PRINTER.
14
15 DATA DIVISION.
16 FILE SECTION.
17 FD RENTAL-FILE
18 RECORD CONTAINS 56 CHARACTERS.
19 01 RENTAL-RECORD.
20 05 REN-CONTRACT-NO PIC 9(6).
21 05 REN-NAME.
22 10 REN-LAST-NAME PIC X(15).
23 10 REN-FIRST-NAME PIC X(10).
Chapter 8 Data Validation
4 Limitations of COBOL-74
ADD 1 TO
MALE-COUNTER
;hart
1
IF VALID-RECORD-SW = Y'
IF SEX = 'M'
ADD 1 TO MALE-COUNTER
END-IF
IF INCOME > 50000
ADD 1 TO HIGH-INCOME-CTR
END-IF
END-IF.
(b) C O B O L - 8 5
IF VALID-RECORD-SW = 'Y'
PERFORM DO-MORE-TESTS.
DO-MORE-TESTS.
IF SEX = 'M'
ADD 1 TO MALE-COUNTER.
IF INCOME > 50000
ADD 1 TO HIGH-INCOME-CTR.
Chapter 9 - More About the Procedure Division
T h e Data Division entries in Figure 9.9a contain several data n a m e s that appear in
both S T U D E N T - R E C O R D a n d P R I N T - L I N E — f o r example, C R E D I T S — a n d a n y
Procedure Division reference to C R E D I T S will produce a compiler error indicating a
nonunique data name. This is because the compiler cannot determine w h i c h
C R E D I T S (in S T U D E N T - R E C O R D or PRINT-LINE) is referenced. O n e solution is
01 STUDENT-RECORD.
05 STUDENT-NAME PIC X(20).
05 SOCIAL-SECURITY-NUM PIC 9(9).
05 STUDENT-ADDRESS.
10 STREET PIC X(15).
10 CITY-STATE PIC X(15).
05 ZIP-CODE PIC X(5).
05 CREDITS PIC 9(3).
05 MAJOR PIC X(10).
05 FILLER PIC X(3).
PRINT-LINE.
10 STUDENT-NAME PIC X(20).
10 FILLER PIC XX.
10 CREDITS PIC ZZ9.
10 FILLER PIC XX.
10 TUITION PIC $$,$$9.99.
10 FILLER PIC XX.
10 STUDENT-ADDRESS.
15 STREET PIC X(15).
15 CITY-STATE PIC X(15).
15 ZIP-CODE PIC X(5).
10 FILLER PIC XX.
10 SOCIAL-SECURITY-NUM PIC 999B99B9999
10 FILLER PIC X(47).
I
MOVE CORRESPONDING STUDENT-RECORD TO PRINT-LINE.
Program Design ..
T h e car billing p r o g r a m has two objectives: to complete the two-program sequence
b e g u n in Chapter 8 a n d to illustrate the Procedure Division statements presented in
this chapter. Both objectives impact the design of the p s e u d o c o d e a n d associated
hierarchy chart.
T h e hierarchy chart in Figure 9.11 is written without the priming read of
earlier programs. T h e highest-level m o d u l e , PREPARE-RENTAL-REPORT, has three
subordinates: G E T - T O D A Y S - D A T E , P R O C E S S - R E N T A L - R E C O R D S , a n d W R I T E -
R E N T A L - T O T A L S . P R O C E S S - R E N T A L - R E C O R D S in turn is the driving m o d u l e of
the p r o g r a m a n d performs four lower-level paragraphs: C O M P U T E - I N D T v T D U A L -
BILL, W R I T E - H E A D I N G - L I N E S , W R I T E - D E T A I L - L I N E , a n d I N C R E M E N T - R E N T A L -
T O T A L S . C O M P U T E - I N D I V I D U A L - B I L L has three subordinate modules, C O M P U T E -
MILEAGE-TOTAL, COMPUTE-DAILY-TOTAL, and C O M P U T E - I N S U R A N C E - T O T A L
to c o m p u t e the c o m p o n e n t s of a customer's bill.
T h e paragraph W R I T E - H E A D I N G - L I N E S is subordinate to P R O C E S S - R E N T A L -
R E C O R D S , w h i c h differs from a n earlier hierarchy chart (page 119) that placed the
heading routine o n a higher level. T h e earlier structure, however, produced only a
single heading at the start of processing, whereas the current requirement is to
produce a heading at the top of every page; hence the heading routine will be
executed several times a n d is subordinate to processing a record.
T h e pseudocode in Figure 9.12 takes advantage of the in-line perforin a n d
false-condition branch to eliminate the priming read used in earlier examples. T h e
pseudocode also implements the required page heading routine b y initializing the
line counter to six a n d testing its value prior to writing each detail line. T h e heading
PREPARE
RENTAL
REPORT
(coniinued)
i
] DO UNTIL valid-field-switch = spaces
| Accept last-name
| J {— IF last-name = spaces
j | Display 'Error - Missing last name'
| j Move 'NO' to valid-field-switch
j I ELSE
S I Move spaces to valid-field-switch
j | — ENDIF
I ENDDO
I . . . Validation checks for remaining fields
| Display information correct message
I p - DO UNTIL valid confirmation ("Y", "y", "N", or "n")
j Accept confirm-switch
I '— IF valid confirmation
Clear previous error message
ELSE
I Display 'Must be "Y" or "N"'
| L . ENDIF
! | — ENDDO
l
- ENDDO
Compute miles driven = miles in - miles out
r - DO CASE
| Car Type E - Move economy rate to mileage rate
Car lype C - Move compact rate to mileage rate
Car Type M - Move midsize to mileage rate
Car Type F - Move full size rate to mileage rate
Car Type L - Move luxury rate to mileage rate
L - END CASE
Compute mileage total = miles driven * mileage rate
Compute daily total = days rented * daily rate
— IF insurance taken
Compute insurance = insurance rate * days rented
—• END-IF
Compute total bill = mileage amount + daily amount + insurance
Display computed bill
Write valid record to valid record file
Display Another record message
— DO UNTIL valid confirmation ("Y", " y " , "N", or "n")
Accept confirm-switch
— IF valid confirmation
Clear previous error message
f ELSE
| Display 'Must be "Y" or "N"'
I — ENDIF
I— ENDDO
- ENDDO
Close valid-rental-file
Stop run
Chapter 10 — Screen l-O
c (continued)
139 05 SCREEN-PROMPTS.
140 10 LINE 1 BLANK LINE BACKGROUND-COLOR MAGENTA. [
141 10 COLUMN 20 VALUE 'Mavis Car Rental Company' ,
142 BACKGROUND -COLOR MAGENTA
143 FOREGROUND -COLOR BRIGHT-GREEN.
144 10 SCR-DATE PIC X(8) FROM SCREEN-DATE
145 COLUMN 55 BACKGROUND-COLOR MAGENTA
146 FOREGROUND-COLOR BRIGHT-GREEN.
147 10 LINE 3 COLUMN 7 VALUE 'Contract No:'.
1
148 J 0 _ LINE 5 COLUMN 7 VALUE 'Customer Information: .
1
149 !
10 LINE 6 COLUMN 9 VALUE 'Last Name . ;
150 ! 10 COLUMN 25 VALUE 'First'. '
151 10 COLUMN 36 VALUE 'Initial'.
152 10 LINE 9 COLUMN 6 VALUE 'Car Information:'.
153 10 LINE 10 COLUMN 12 VALUE 'Type Code:'.
154 10 COLUMN 25
155 VALUE '(Compact, Economy, Midsize, Fullsize, Luxury)'
156 FOREGROUND -COLOR CYAN.
157 10 COLUMN 26 VALUE ' C HIGHLIGHT.
158 10 COLUMN 35 VALUE ' E' HIGHLIGHT.
159 10 COLUMN 44 VALUE 'M' HIGHLIGHT.
160 10 COLUMN 53 VALUE 'F' HIGHLIGHT.
161 10 COLUMN 63 VALUE 'L' HIGHLIGHT.
162 10 LINE 11 COLUMN 8 VALUE 'Date Returned:'.
163 10 COLUMN 23 VALUE 'mm/dd/yy'
164 FOREGROUND -COLOR BRIGHT-WHITE.
165 10 LINE 12 COLUMN 10 VALUE 'Days Rented:'.
166 10 LINE 13 COLUMN 10 VALUE 'Mileage:'.
167 10 LINE 14 COLUMN 13 VALUE 'Miles In:'.
168 10 LINE 15 COLUMN 12 VALUE 'Miles Out:'.
169 10 COLUMN 37 VALUE 'Mileage Rate:'.
170 10 LINE 16 COLUMN 12 VALUE ' Insurance:'.
171 10 COLUMN 25 VALUE '(Y/N)'
172 FOREGROUND--COLOR CYAN.
173
174 05 SCREEN-INPUTS.
175 10 SCR-CONTRACT-NO PIC 9(6) USING REN-CONTRACT-NO
176 LINE 3 COLUMN 20 REVERSE-VIDEO.
177 10 SCR-LAST-NAME PIC X(15) USING REN-LAST-NAME
178 LINE 7 COLUMN 9 REVERSE-VIDEO.
179 10 SCR-FIRST-NAME PIC X( 10) USING REN-FIRST-NAME
180 LINE 7 COLUMN 25 REVERSE-VIDEO.
181 10 SCR-INITIAL PIC X USING REN-INITIAL '
<
182 LINE 7 COLUMN 36 REVERSE-VIDEO.
183 10 SCR-CAR-TYPE PIC X USING REN-CAR-TYPE
184 LINE 10 COLUMN 23 REVERSE-VIDEO AUTO.
185 10 SCR-RETURNED-MONTH PIC 99 USING REN-RETURNED-MONTH
186 LINE 11 COLUMN 23 REVERSE-VIDEO AUTO. t
BENJAMIN, L 05111A3222A2333A3444A3555B3
BORROW, J 04666B3777B3888B3999B4
MILGROM, M 06123C4456C4789C4012C4345C3678C4
(a) Test Data
AVERAGE: 3.00
AVERAGE: 2.00
(b) individual Transcripts
BENJAMIN, L 5 14 53 3.79
05 ST-NUMBER-OF-COURSES PIC 99
05 ST-COURSE-INFO OCCURS 1 TO 8 TIMES
DEPENDING ON ST-NUMBER-OF-COURSES.
10 ST-COURSE-NUMBER PIC X(3).
10 ST-COURSE-GRADE PIC X.
10 ST-COURSE-CREDITS PIC 99
PERFORM WRITE-COURSE-DATA
VARYING COURSE-SUBSCRIPT FROM 1 BY 1
UNTIL COURSE-SUBSCRIPT > ST-NUMBER-OF-COURSES.
WRITE-COURSE-DATA.
MOVE ST-COURSE-NUMBER (COURSE-SUBSCRIPT) TO PL-NUMBER.
MOVE ST-COURSE-GRADE (COURSE-SUBSCRIPT) TO PL-GRADE.
WRITE PRINT-LINE FROM PRINT-LINE-ONE
AFTER ADVANCING 1 LINE.
fb) PERFORM V A R Y I N G
MOVE 1 TO COURSE-SUBSCRIPT.
PERFORM WRITE-COURSE-DATA ST-NUMBER-OF-COURSES TIMES.
WRITE-COURSE-DATA.
MOVE ST-COURSE-NUMBER (COURSE-SUBSCRIPT) TO PL-NUMBER.
MOVE ST-COURSE-GRADE (COURSE-SUBSCRIPT) TO PL-GRADE.
WRITE PRINT-LINE FROM PRINT-LINE-ONE
AFTER ADVANCING 1 LINE.
ADD 1 TO COURSE-SUBSCRIPT.
(c) P E R F O R M T I M E S
Chapter 11 Introduction to Tables
PERFORM WRITE-COURSE-DATA
VARYING COURSE-INDEX FROM 1 BY 1
UNTIL COURSE-INDEX > ST-NUMBER-OF-COURSES.
WRITE-COURSE-DATA.
MOVE ST-COURSE-NUMBER (COURSE-INDEX) TO PL-NUMBER.
MOVE ST-COURSE-GRADE (COURSE-INDEX) TO PL-GRADE.
WRITE PRINT-LINE FROM PRINT-LINE-ONE
AFTER ADVANCING 1 LINE.
(b) P E R F O R M V A R Y I N G
WRITE-COURSE-DATA.
MOVE ST-COURSE-NUMBER (COURSE-INDEX) TO PL-NUMBER.
MOVE ST-COURSE-GRADE (COURSE-INDEX) TO PL-GRADE.
WRITE PRINT-LINE FROM PRINT-LINE-ONE
AFTER ADVANCING 1 LINE.
SET COURSE-INDEX UP BY 1.
(c) P E R F O R M T I M E S
COBOL-85 introduced several minor changes in conjunction with table
processing. The new compiler allows seven levels of subscripting as opposed
to the earlier limit of three, but given that the typical programmer seldom uses
three-level tables, this extension is of little practical benefit. (Multiple-level
tables are covered in Chapter 13.) The OCCURS DEPENDING ON clause
may specify a value of zero, whereas at least one occurrence was required in
COBOL-74.
A more significant change is the introduction of relative subscripting
(as explained in Figure 11.6), enabling the reference DATA-NAME
(SUBSCRIPT + integer). Relative subscripting was not permitted in COBOL-
74 (although relative indexing was).
P R O G R A M M I N G T I P
Data names defined as switches and/or subscripts should be restricted to a single use. Consider:
Poor Code
PERFORM INITIALIZE-TITLE-FILE
UNTIL EOF-SWITCH = 'YES'.
MOVE SPACES TO EOF-SWITCH.
PERFORM PROCESS-EMPLOYEE-RECORDS
1
UNTIL EOF-SWITCH = 'YES .
PERFORM COMPUTE-SALARY-HISTORY
VARYING SUBSCRIPT FROM 1 BY 1
UNTIL SUBSCRIPT > 3.
PERFORM FIND-MATCH-TITLE
VARYING SUBSCRIPT FROM 1 BY 1
UNTIL SUBSCRIPT > 100.
improved ooae
01 PROGRAM-SUBSCRIPTS.
05 TITLE-SUBSCRIPT PIC S9(4) COMP.
05 SALARY-SUBSCRIPT PIC S9(4) COMP.
01 END-OF-FILE-SWITCHES.
05 END-OF-TITLE-FILE-SWITCH PIC X(3) VALUE SPACES.
05 END-OF-EMPLOYEE-FILE-SWITCH PIC X(3) VALUE SPACES.
PERFORM INITIALIZE-TITLE-FILE
UNTIL END-OF-TITLE-FILE-SWITCH = 'YES'.
PERFORM PROCESS-EMPLOYEE-RECORDS
UNTIL END-OF-EMPLOYEE-FILE-SWITCH = 'YES'.
PERFORM COMPUTE-SALARY-HISTORY
VARYING SALARY-SUBSCRIPT FROM 1 BY 1
UNTIL SALARY-SUBSCRIPT > 3.
PERFORM FIND-MATCHING-TITLE
VARYING TITLE-SUBSCRIPT FROM 1 BY 1
UNTIL TITLE-SUBSCRIPT > 100.
At the very least, the improved code offers superior documentation. By restricting data names to a single
use, one automatically avoids such nondescript entries as EOF-SWITCH or SUBSCRIPT. Of greater impact,
the improved code is more apt to be correct in that a given data name is modified or tested in fewer places
within a program. Finally, if bugs do occur, the final values of the unique data names (TITLE-SUBSCRIPT and
SALARY-SUBSCRIPT) will be of much greater use than the single value of SUBSCRIPT.
Chapter 12 — Table Lookups
^i"' (continued)
(continued)
01 COMPANY-TOTAL-LINE.
05 FILLER PIC X(31) VALUE SPACES.
05 FILLER PIC X(25)
VALUE COMPANY TOTAL
05 COMPANY-SALES-TOTAL PIC $Z(3),ZZ9-.
05 FILLER PIC X(5) VALUE SPACES.
05 COMPANY-COMM-TOTAL PIC $Z(3),ZZ9-.
05 FILLER PIC X(51) VALUE SPACES.
PROCEDURE DIVISION.
0000-SORT-SALES-RECORDS.
|100-CALCULATE-COMMISSION.
' OPlN^NPUrSALES^FILE^ INPUT zDUF
PERFORM UNTIL NO-DATA-REMAINS
READ SALES-FILE INTO SALES-RECORD-IN
AT END
MOVE 'NO' TO DATA-REMAINS-SWITCH
NOT AT END
COMPUTE SR-COMMISSION ROUNDED =
SR-SALES * SR-COMMISSION-PERCENT
SIZE ERROR DISPLAY 'ERROR ON COMMISSION FOR
SR-NAME
END-COMPUTE /'
I IF SR-COMMISSION > 100
RELEASE SORT-RECORD FROM SALES-RECORD-IN
END-IF
END-READ
END-PERFORM.
CLOSE SALES-FILE.
! 200-PREPARE-COMMISSION-REPORT.
OPEN OUTPUT PRINT-FILE. ::EDUR
PERFORM 230-GET-TODAYS-DATE.
salesperson a n d then adding, or rolling, the salesperson total to the c o m p a n y total.
T h e latter is m o r e efficient in that fewer additions are performed.
Similar reasoning applies to the two-level report of Figure 15.3b, in w h i c h the
location total c a n be c o m p u t e d two different w a y s — b y adding the value of each
incoming transaction to a running location total, or b y waiting for a control break
o n salesperson, then rolling the salesperson total to the location total. In similar
fashion, the c o m p a n y total m a y be obtained in three ways. First, b y adding the
value of every incoming transaction to a running c o m p a n y total. Second, b y rolling
the salesperson total into the c o m p a n y total after a one-level break o n salesperson.
O r third, b y rolling the location total into the c o m p a n y total after a two-level break
o n location. T h e third approach is the m o s t efficient.
Y o u should be able to extend this logic to the three-level report of Figure 15.4b,
w h i c h maintains a running total for each salesperson, then roils the salesperson
total into the location total (after a break o n salesperson), rolls the location total
into the region total (after a break o n location), a n d finally rolls the region total into
the c o m p a n y total (after a break o n region).
(continued)
REGION: SOUTHEAST
LOCATION: ST. PETERSBURG
REGION: NORTHEAST
LOCATION: BALTIMORE
REGION: MIDWEST
LOCATION: CHICAGO
SALESPERSON: BENWAY
ACCOUNT t SALES COMMISSION
U U U U U 3 231- 23-
476530 235- 12-
988888 450 5
999340 334 100
SALESPERSON: HUMMER
ACCOUNT # SALES COMMISSION
000100 107- 5-
649356 345 17
694446 904 90
i:
Chapter 15 — Control Break
44
45 WORKING-STORAGE SECTION.
46 01 FILLER PIC X(14)
47 VALUE 'WS BEGINS HERE'.
48
49 01 SALES-RECORD-IN.
50 05 SR-ACCOUNT-NUMBER PIC 9(6).
51 05 FILLER PIC X.
52 05 SR-NAME PIC X(15).
53 05 SR-SALES PIC S9(4).
54 05 FILLER PIC XX.
55 05 SR-COMMISSION-PERCENT PIC V99.
ETC
05 FILLER PIC XX.
uu
57 05 SR-LOCATION PIC X(15).
58 05 SR-REGION PIC X(ll).
59
60 01 PROGRAM-SWITCHES-AND-COUNTERS
si DATA-REMAINS-SW i ir
V X
\j ~> PIC X(3) U A I
KttLUt'YES'.
62 88 NO-DATA-REMAINS VALUE 'NO' .
63 05 PREVIOUS-NAME PIC X(15) VALUE SPACES.
64 05 PAGE-COUNT PIC 99 VALUE ZEROES.
65
66 01 CONTROL-BREAK-TOTALS.
67 05 INDIVIDUAL-TOTALS.
68 10 IND-COMMISSION PIC S9(4).
69 05 SALESPERSON-TOTALS.
70 10 SALESPERSON-SALES-TOT PIC S9(6).
71 10 SALESPERSON-COMM-TOT PIC S9(6).
72 05 COMPANY-TOTALS.
73 10 COMPANY-SALES-TOT PIC S9(6) VALUE ZEROS.
74 10 COMPANY-COMM-TOT PIC S9(6) VALUE ZEROS.
75
76 01 REPORT-HEADING-LINE.
77 05 FILLER PIC X(25) VALUE SPACES.
78 05 FILLER PIC X(21)
79 VALUE 'SALES ACTIVITY REPORT'.
80 05 FILLER PIC X(19) VALUE SPACES.
81 05 FILLER PIC X(5) VALUE 'PAGE '
82 05 HDG-PAGE PIC Z9.
83 05 FILLER PIC X(60) VALUE SPACES.
84
85 01 SALESPERSON-HEADING-LINE-ONE.
86 05 FILLER PIC X(15) VALUE SPACES.
87 05 FILLER PIC X(13)
88 VALUE 'SALESPERSON: '.
89 05 HDG-NAME PIC X(15).
90 05 FILLER PIC X(89) VALUE SPACES.
91
92 01 SALESPERSON-HEADING-LINE-TWO.
93 05 FILLER PIC X(23) VALUE SPACES.
Chapter
CI (continued)
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT SALES-FILE ASSIGN TO 'A:\CHAPTR15\S0RTIN.DAT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT PRINT-FILE
ASSIGN TO PRINTER.
SELECT SORT-WORK-FILE
ASSIGN TO 'A:\CHAPTR15\S0RTWK.DAT'.
SELECT SORTED-SALES-FILE ASSIGN TO 'A:\CHAPTR15\S0RT0UT.DAT
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD SALES-FILE
RECORD CONTAINS 58 CHARACTERS
DATA RECORD IS SALES-RECORD.
01 SALES-RECORD PIC X(58).
FD PRINT-FILE
RECORD CONTAINS 132 CHARACTERS
DATA RECORD IS PRINT-LINE.
01 PRINT-LINE PIC X(132).
SD SORT-WORK-FILE
RECORD CONTAINS 58 CHARACTERS
DATA RECORD IS SORT-RECORD.
01 SORT-RECORD.
05 SORT-ACCOUNT-NUMBER PIC 9(6).
05 FILLER PIC X.
05 SORT-NAME PIC X(15).
05 FILLER PIC X(10).
05 SORT-LOCATION PIC X(15).
05 SORT-REGION PIC X(ll).
FD SORTED-SALES-FILE
RECORD CONTAINS 58 CHARACTERS
DATA RECORD IS SORTED-SALES-RECORD.
01 SORTED-SALES-RECORD PIC X(58).
WORKING-STORAGE SECTION.
01 FILLER PIC X(14)
VALUE "WS BEGINS HERE'.
01 SALES-RECORD-IN.
05 SR-ACCOUNT-NUMBER PIC 9(6).
05 FILLER PIC X.
05 SR-NAME PIC X(15).
C h a p t e r 1 5 Control Breaks
lure I S . t O (continued)
194 ! SORT-LOCATION
195 SORT-NAME
196 WITH DUPLICATES IN ORDER
197 USING SALES-FILE Keys in SORT statement match control breaks
213
214 260-WRITE-C0MPANY-T0TAL.
215 MOVE COMPANY-SALES-TOT TO COMPANY-SALES-TOTAL.
216 MOVE COMPANY-COMM-TOT T O COMPANY-COMM-TOTAL.
217 WRITE PRINT-LINE FROM COMPANY-TOTAL-LINE
218 AFTER ADVANCING 2 LINES.
219
220 300-INITIALIZE-REGION.
221 MOVE SR-REGION TO PREVIOUS-REGION.
222 INITIALIZE REGION-TOTALS.
223
224 320-WRITE-REGION-HEADING.
225 A D D 1 TO PAGE-COUNT.
226 MOVE PAGE-COUNT TO HDG-PAGE.
227 W R I T E P R I N T - L I N E FROM REPORT-HEADING-LINE
228 AFTER ADVANCING PAGE.
229 MOVE SR-REGION TO HDG-REGION.
230 WRITE PRINT-LINE FROM REGION-HEADING-LINE
231 AFTER ADVANCING 2 LINES.
232
233 340-PROCESS-ONE-LOCATION.
234 PERFORM 400-INITIALIZE-LOCATION.
235 PERFORM 420-WRITE-L0CATI0N-HEADING.
236 PERFORM 440-PR0CESS-0NE-SALESPERS0N
237 UNTIL SR-LOCATION N O T EQUAL PREVIOUS-LOCATION
238 O R SR-REGION N O T EQUAL PREVIOUS-REGION
239 OR NO-DATA-REMAINS.
240 PERFORM 460-WRITE-L0CATI0N-T0TAL.
C h a p t e r 1 6 — Subprograms
1 IDENTIFICATION DIVISION.
2 PROGRAM- ID. INPUTSUB. _
3 AUTHOR. CVV.
4
5 DATA DIVISION.
6 WORKING-STORAGE SECTION.
7 01 FILLER PIC X(38)
8 VALUE 'WS BEGINS HERE FOR SUBPROGRAM INPUTSUB'.
9
10 01 PROGRAM-SWITCHES.
11 05 VALID-FIELD-SWITCH PIC XX.
12 88 VALID-FIELD VALUE SPACES.
13 05 CONFIRM-SWITCH PIC X.
14 88 ALL-DATA-VALID VALUE 'Y' 'y'.
15
16 COPY COLORCPY.
STCPY> 01 SCREEN-COLORS. PIC S9(4) C0MP-5.
18 * COLORS FOR FOREGROUND AND BACKGROUND
19 78 BLACK VALUE 0.
20 78 BLUE VALUE 1.
21 78 GREEN VALUE 2.
22 78 CYAN VALUE 3.
23 78 RED 4.
24 78 MAGENTA 5.
25 78 BROWN 6.
26 78 WHITE 7.
27 * ADDITIONAL COLORS FOR FOREGROUND ONLY
28 78 BRIGHT-BLACK 8.
29 78 BRIGHT-BLUE 9.
30 78 BRIGHT-GREEN 10.
31 78 BRIGHT-CYAN 11.
i r e 16,13 COBOL Programs for Problem 4
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINPROG.
WORKING-STORAGE SECTION.
COPY INPUTREC
01 INPUT-DATA.
05 INPUT-NAME PIC X(15).
01 PASSED-PARAMETERS
05 PARM-A PIC 9(4).
05 PARM-B PIC XX.
PROCEDURE DIVISION.
IDENTIFICATION DIVISION.
PROGRAM-ID. PROGA
LINKAGE SECTION.
COPY INPUTREC
01 INPUT-DATA.
05 INPUT-NAME PIC X(15).
01 NEW-DATA-NAMES.
05 NEW-NAME-A PIC XX.
05 NEW-NAME-B PIC 9(4).
PROCEDURE DIVISION
USING NEW-NAME-A, NEW-NAME-B, INPUT-DATA.
(b) First Subroutine
IDENTIFICATION DIVISION.
PROGRAM-ID. PROG-B.
LINKAGE SECTION.
01 PASSED-PARAMETERS.
05 PARM-A PIC 9(4).
PROCEDURE DIVISION
USING PARM-A.
(b) Second Subroutine
C h a p t e r 17 — Sequential File Maintenance
'igure 1 7 . S (continued)
I
800000000VILLAR C
999999999GILLENS0N MANYC10 c
A !
(a) Transaction File
O0O00OOOOBOROW JSATL07 A
O0OO0OO0OBOROW JS 10000C
O000OO0OOBOROW JS 20000C
400000000MOLDOF BLATL15 A
444444444RICHARDS IM 05000C
700000000MILGROM A D
800000000VAZQUEZ C 55000C
999999999GILLENS0N MANYC10 A
EDIT
TRANSACTION
FILE
PROCESS
TRANSACTIONS
DO VALID DO DO DO WRITE
DO SEQUENCE
CODE ADDITION CORRECTION VALID
CHECK
CHECK CHECKS CHECKS TRANSACTION
WRITE ERROR WRITE ERROR WRITE ERROR WRITE ERROR WRITE ERROR
MESSAGE MESSAGE MESSAGE MESSAGE MESSAGE
Chapter 17 Sequential File Maintenance
89 PROCEDURE DIVISION.
90 100-EDIT-TRANSACTION-FILE.
91 OPEN INPUT TRANSACTION-FILE
92 OUTPUT VALID-TRANSACTION-FILE
93 ERROR-FILE.
94 PERFORM UNTIL WS-EOF-SWITCH = 'YES'
95 READ TRANSACTION-FILE INTO WS-TRANSACTION-RECORD
96 AT END
97 MOVE 'YES' TO WS-EOF-SWITCH
98 NOT AT END
99 PERFORM 210-PR0CESS-TRANSACTI0NS
100 END-READ
101 END-PERFORM.
102 CLOSE TRANSACTION-FILE
103 VALID-TRANSACTION-FILE
104 ERROR-FILE.
105 STOP RUN.
106
107 210-PR0CESS-TRANSACTI0NS.
108 MOVE 'YES' TO WS-VALID-RECORD-SWITCH.
109 PERFORM 300-DO-SEQUENCE-CHECK.
110 PERFORM 310-DO-VALID-CODE-CHECK.
111 IF ADDITION
112 PERFORM 320-DO-ADDITION-CHECKS
113 ELSE
114 IF CORRECTION
115 PERFORM 330-D0-C0RRECTI0N-CHECKS
116 END-IF
117 END-IF.
118 IF WS-VALID-RECORD-SWITCH = 'YES'
119 PERFORM 340-WRITE-VALID-TRANSACTION
120 END-IF. to output file
121
122 300-DO-SEQUENCE-CHECK.
123 IF TR-SOC-SEC-NUMBER < WS-PREVIOUS-SOC-SEC-NUMBER
124 MOVE 1 TO WS-ERROR-CODE
125 PERFORM 400-WRITE-ERR0R-MESSAGE~^-~^_^
126 END-IF.
127 MOVE TR-SOC-SEC-NUMBER TO WS-PREVIOUS-SOC-SEC-NUMBER.
128
129 310-DO-VALID-CODE-CHECK.
130 IF NOT VALID-CODES • '
131 IMOVE 2 TO WS-ERROR-CODE
132 PERFORM 400-WRITE-ERROR-MESSAGE
133 END-IF.
134
135 320-DO-ADDITION-CHECKS.
Chapter 17 Sequential File Maintenance
0OO0000OOBOROW JSATL07 A
| O0O0OOOO0B0ROW JS 10000C
j OOOOOOOOOBOROW JS 20000C
j 400000000MOLDOF BLATL15 A
| 444444444RICHARDS IM 050O0C
j 700000000MILGR0M A D
1 800000000VAZQUEZ C 55000C
j 999999999GILLENS0N MANYC10 A
OOOOOOOOOBOROW JSATL0700030000
! 100000000GRABER P ATLI500000000
200000000RUBIN MAB0S0800020000
300000000ANDERSON IRBOS1000113000
400000000MOLDOF BLATL1500000000
500000000GLASSMAN JSNYC1000045000
600000000GRAUER RTNYC0800087500
800000000VAZQUEZ C ATL1200115000
900000000CLARK E NYC0700002500
999999999GILLENS0N MANYC1000000000
set contains the highest key in the corresponding control area; thus 377, 619, a n d
800 are the highest keys in thefirst,second, a n d third control areas, respectively.
E a c h control area has its o w n sequence set. T h e entries in thefirstsequence set
s h o w the highest keys of the control intervals in thefirstcontrol area to be 280, 327,
a n d 377, respectively. Note that the highest entry in the third control interval, 377,
corresponds to the highest entry in thefirstcontrol area of the index set.
Figure 18.1 illustrates two kinds of pointers, vertical a n d horizontal. Vertical
pointers are used for direct access to a n individual record. For example, a s s u m e
that the record with a key of 449 is to be retrieved. V S A M begins at the highest level
of index (that is, at the index set). It concludes that record key 449, if it is present, is
in the second control area (377 is the highest key in thefirstarea, whereas 619 is the
highest key in the second control area). V S A M follows the vertical pointer to the
sequence set for the second control area a n d draws itsfinalconclusion: record key
449, if it exists, will be in thefirstcontrol interval of the second control area.
Horizontal pointers are used for sequential access only. In this instance, V S A M
begins at the first sequence set a n d uses t h e horizontal pointer to get from that
sequence set record to the o n e containing the next highest key. Put another way, the
vertical pointer in a sequence set points to data; the horizontal pointer indicates the
sequence set containing the next highest record.
Figure 18.1 c o n t a i n s several allocations of free space, w h i c h are distributed in
o n e of two ways: as free space within a control interval or as a free control interval
within a control area. In other words, as V S A M loads a file, e m p t y space is deliberately
left throughout the file. This is d o n e to facilitate subsequent insertion of n e w records.
Figure 18.2 s h o w s the changes brought about b y the addition of two n e w
records, with keys of 410 a n d 730, to the file of Figure 18.1. Addition of the first
record, key 410, poses n o problem, as free space is available in the control interval
Horiz. Horiz.
Sequence Set Pntr. W Sequence Set Pntr. Sequence Set
Vert. Vert. Vert. Vert. Vert. Vert. Vert. Vert.
280 327 377 469 619 700 800
Pntr. Pntr. Pntr. Pntr. Pntr. Pntr. Pntr. Pntr.
7 7 7
251 312 345
7 7 394 500
7 7 7
627 717 746
Control Interval
Split
In t h i s e x a m p l e C U S T O M E R - L O A N - N U M B E R is a g r o u p i t e m a n d c o n s i s t s o f t h e
. :* / * ' ! I f ' I V MV * 1 ' ! > \ ! I I \ « 111 *I I I I / U \ ' X I I I \ .1 1 1 1 * l > I 1-.- i' . I
c i c i i i c n i a i y nciiis, i j u j 1 u i v i m \ - i i u i v i D m d i m n / m \ - , \ u i v u n c v e i y v a l u e ui m e
r e c o r d k e y ( C U S T O M E R - L O A N - N U M B E R ) m u s t b e u n i q u e , b u t t h e r e c a n b e several
l o a n s for t h e s a m e c u s t o m e r , w i t h e a c h l o a n a s s i g n e d a n e w l o a n n u m b e r . C u s t o m e r
1 1 1 1 1 1 , for e x a m p l e , m a y h a v e t w o o u t s t a n d i n g l o a n s , w i t h r e c o r d k e y s o f 1 1 1 1 1 1 0 0 1
a n d 1 1 1 1 1 1 0 0 4 , r e s p e c t i v e l y . (Loans 0 0 2 a n d 0 0 3 m a y h a v e b e e n p r e v i o u s l y p a i d
off.) T h e p r o b l e m is t o retrieve all l o a n s for a g i v e n c u s t o m e r , w h i c h l e a d s t o a
d i s c u s s i o n o f t h e START s t a t e m e n t .
Ttw Statement
T h e START s t a t e m e n t m o v e s n o n s e q u e n t i a l l y ( r a n d o m l y ) i n t o a n i n d e x e d file t o
t h e first r e c o r d w h o s e v a l u e is e q u a l t o , greater t h a n , o r n o t l e s s t h a n t h e v a l u e
c o n t a i n e d i n t h e identifier. T h e INVALID KEY c o n d i t i o n is r a i s e d if t h e file d o e s
n o t c o n t a i n a r e c o r d m e e t i n g t h e s p e c i f i e d criterion. S y n t a c t i c a l l y , t h e START
s t a t e m e n t h a s t h e form:
IS EQUAL TO
IS =
IS GREATER THAN
IS >
START file- name KEY identifier
IS NOT LESS THAN
IS NOT <
IS GREATER THAN OR EQUAL TO
IS >=
[END-START]
T h e START s t a t e m e n t c a n b e u s e d i n c o n j u n c t i o n w i t h a c o n c a t e n a t e d k e y a s
s h o w n i n Figure 18.13. Note, however, that START only moves to the designated
record, but does not read the record. I n o t h e r w o r d s , a READ s t a t e m e n t is r e q u i r e d
i m m e d i a t e l y f o l l o w i n g START. T h e s u b s e q u e n t PERFORM s t a t e m e n t will t h e n
retrieve all l o a n s for t h e c u s t o m e r i n q u e s t i o n .
The READ, DELETE, WRITE, REWRITE, and START statements contain both
an optional scope terminator and a false-condition branch. As indicated
throughout the text, these elements are new to COBOL-85 and were not
available in COBOL-74.
Sixteen I/O status codes (i.e., the majority of the entries in Table
18.1) are new to COBOL-85. The new codes (02, 04, 05, 07, 15, 24, 25,
34, 35, 37, 38, 39, 41, 42, 43, 46, and 49) were added to eliminate the
need for vendor-specific file status codes that treated the same error
condition in different ways.
C h a p t e r 20 — Object-Oriented COBOL Programming
«• T h e I n s t a n c e d o e s n o t h a v e s p e c i a l d a t a o t h e r t h a n that n e e d e d t o m a n a g e
LllC lllC
» T h e o n l y o b j e c t that it is k n o w s is S t u d e n t .
« W h a t it d o e s is t o r e a d r e c o r d s f r o m t h e file, test t o s e e w h e t h e r t h e y m e e t t h e
s p e c i f i c a t i o n s , a n d c r e a t e S t u d e n t i n s t a n c e s c o n t a i n i n g t h e S t u d e n t data.
Of t h e t h r e e m e t h o d s d e f i n e d for t h e i n s t a n c e , o n l y o n e G e t S t u d e n t is i n v o k e d
f r o m o u t s i d e . T h e o t h e r t w o m e t h o d s are "private" m e t h o d s t o b e u s e d o n l y b y
other m e t h o d s in the StudentDM.
B e g i n n i n g at line 6 5 , G e t S t u d e n t m a i n t a i n s a L o c a l - s t o r a g e S e c t i o n t o h o l d a n
e n d - o f - f i l e s w i t c h a n d a Linkage S e c t i o n t o r e c e i v e t h e i n v o k i n g p a r a m e t e r s a n d t o
p a s s b a c k t h e h a n d l e for t h e S t u d e n t i n s t a n c e .
T h e P r o c e d u r e D i v i s i o n h a s b o t h u s i n g a n d r e t u r n i n g c l a u s e s to a l l o w u s e o f
t h e i t e m s i n t h e Linkage S e c t i o n . T h e a l g o r i t h m o f t h e m e t h o d is to initialize t h e
Student handle to null a n d t h e n invoke t h e ReadRecord m e t h o d . T h e invoke
s t a t e m e n t u s e s t h e k e y w o r d s e l f. Sel f refers t o t h e p o i n t e r o f t h e current i n s t a n c e .
A n i n s t a n c e a l w a y s k n o w s its o w n m e m o r y l o c a t i o n a n d c a n r e f e r e n c e t h a t l o c a t i o n
b y u s i n g s e l f.
O n c e t h e r e c o r d is read, G e t S t u d e n t e v a l u a t e s t h e c r e d i t h o u r s a g a i n s t t h e
m i n i m u m a n d t h e m a j o r a g a i n s t t h e r e q u i r e d major. If t h e t e s t is s u c c e s s f u l , t h e
r o u t i n e i n v o k e s t h e C r e a t e S t u d e n t m e t h o d , a n d u s e s t h e result t o return t h e S t u d e n t
H a n d l e a n d t o s t o p t h e l o o p . If t h e t e s t w a s n o t s u c c e s s f u l , t h e r o u t i n e i n v o k e s
ReadRecord m e t h o d to get t h e next record. W h e n ReadRecord runs o u t of records,
it returns "NO" t o t h e D a t a - R e m a i n s - S w i t c h . T h e l o o p s t o p s , a n d b e c a u s e n o i n s t a n c e
o f S t u d e n t h a s b e e n c r e a t e d t h e m e t h o d r e t u r n s a n u l l h a n d l e to t h e i n v o k i n g
procedure.
P R O G R A M M I N G T I P
The C O B O L Evaluate statement is m u c h more powerful than C A S E statements in other languages. In most
languages, the C A S E statement can test a condition for only one variable at a time. The Evaluate statement,
however, allows testing of several conditions at once. Consider the Eval uate statement in the GetStudent
method of StudentDM.
Aiilftl i* rfMrie-,
H&mtm m y typing
Ui»nge it fa thin:
If H the"
Find options is the third option available in this section. Again, Find options is
generally best left alone by the student p r o g r a m m e r . Figure A.51 s h o w s the C O B O L
data item finds. O n c e p r o g r a m m e r s have b e c o m e experienced in debugging
programs, they m a y w a n t to explore s o m e of the options here.
P Hide sn Hun
"~ F}aiilfat k >»«
r &st threshold levcJ
f* finely ;?e
0fe I Hrfe
Appendix B Getting Started
Click o n the R u n option from the Start M e n u s h o w n in Figure B.la. This action
brings u p the W i n d o w s h o w n in Figure B.lb. In this example, "D:" refers to the C D -
R O M drive. O n your m a c h i n e the C D - R O M m a y have s o m e other letter. If y o u are
installing from diskettes, y o u probably w o u l d use A: instead off):. "Setup.exe" is the
n a m e of the program to install Personal C O B O L . Click o n the O K button a n d the
W e l c o m e message box s h o w n in Figure B.lc appears. (Tick o n O K again.
0 m*
ZD-Rom dn
jP
yaw tjm&iHm
I ' I F I ! IFW ILK !RL«LI(N IFF I'LHIH ULF INTT^LWJFFT
FM.TTL' Y O U I TTF» P I R T I »HT* I .HL< « J I H A I I U I I )
•/(HI ILL *K»T W 4 H I !TI iftdJflB THIS S N F L V I W nvm
M I L N M ^ITLUJI ••ITM •! F I U MFH