COBOL .

COBOL Comments and Questions

Over the years that FluffyCat.com has been online (since 1998!) I've had many COBOL comments and questions emailed to me that don't quite fit into any other category. I'll share a few here, with the senders' names and email addresses removed. Please feel free to leave new comments or questions here using the handy "Add A Comment" feature.

Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by TheMadProfessor on 2014-02-18 Rate this Comment

Regarding the question on ACCEPT/DISPLAY: These two verbs are designed for simple I/O (actually, the simplest possible I/O) of low volume data.

ACCEPT is for data input and the format is 'ACCEPT variable-name [FROM mnemonic-name]' - If FROM is omitted, the data is received from a default device. For example, on an IBM mainframe this is usually SYSIN, on PCs the keyboard. If FROM is specified, it is some device or global variable defined to the platform. The most common such mnemonics usually encountered are DAY, DATE, DAY-OF-WEEK and TIME, but the actual format of the system date and/or time information obtained is system-dependent. Other environment variables may also exist, depending upon the platform. Examples frequently available are PATH, CLASSPATH, LIBPATH, SYSLIB, STEPLIB, etc.

DISPLAY is similar, but for output and the format is 'DISPLAY variable-name [UPON mnemonic-name]' - Again, if UPON is omitted, the data is sent to a default device, usually a printer or the monitor. To the best of my knowledge, specific positioning of the data on the device is not possible.

Comment by TheMadProfessor on 2007-11-30 Rate this Comment

Probably comes too late for your immediate needs (if you have a time-critical question, feel free to email directly at DeMadProf (at) aol (dot) com) but here goes. Expanding on Larry's comments (my name too, oddly enough):

1. a) 'ALL' is invalid in this context, statement would be valid if omitted b) 'ROUNDED' invalid, would be valid if followed B field instead c) invalid because no target field for the result specified. d) Valid as written
2. From the potential answers, I suspect FIELD-A is unsigned with initial value 5 and FIELD-B signed with initial value -10. If so, the result of adding is -5 but the sign is lost when stored in FIELD-A, so answer A (5) is correct.
3. a) is incorrect since inline PERFORMs are usually terminated this way. b) is incorrect since, while they can be terminated with a period, they do not have to be. (As a side comment about the first two answers, you can terminate with an END-PERFORM, period or both but you must specify at least one of them.) c) is incorrect since there is no paragraph name specified to transfer to (if there were, it would not be an inline PERFORM). d) Not absolutely true since there is a practical limit based on computer memory size but most likely the answer intended e) True only if 'any number of statements' was intended literally.
4. Larry had it right.
5. a) Unexecutable as written - would execute correctly if specified 'UNTIL COUNT > 5' (since then same as B); b)This is the correct answer; c) Would either be an endless loop or only perform loop once depending on value of WORK-INDEX; d) Would not execute the loop at all; e) Would only perform loop 4 times. As a side comment, did the original question state something like 'ADD YEARS-WORKED (COUNT) TO WORK-TOTALS'? If not, the PERFORM could have been replaced with a far simpler statement such as 'MULTIPLY YEARS-WORKED BY 5 GIVING WORK-TOTALS'
6. Again, Larry had it.
7. Assuming that '*' designated a space, FIELD-B would contain 'BILLY', FIELD-C '.S.' and FIELD-D 'BARNES', so none of the listed answers were correct but B is closest.
8. False, as written would display as '-435.50DB'. If the picture clause had been '$$$,$$$.99DB', then the answer would have been True.
9. Without seeing the flowchart to be sure, my order would be 1, 7, 4 (typically, no need to format and write output if the input record is rejected), 6, 5 (assuming you would have to create the field only if the input data moved was insufficient somehow), 3, 2.
10. Only ones I can think of offhand are '-' (fixed or floating), CR and DB. (CR/DB are mutually exclusive. When specified, they resolve to spaces if positive and the literal value if negative. Depending on what the number represents in the real world, a negative value could be either a credit or a debit.) Some methods used in other programming languages include display in alternate colors or value enclosed in parenthesis but these are not (AFAIK) available in standard COBOL - nonstandard types (such as Microfocus Cobol or Visual Cobol) might be another story.
11. Ones that come to mind are SPACE, ZERO, HIGH-VALUE, LOW-VALUE, QUOTE along with their plurals. ALL combined with a literal (such as ALL 'T') might also be included in certain context.
12. Not sure on this one...perhaps there is more to the question?
13. a) Alphanumeric fields have low-order truncation (i.e. rightmost characters truncated) unless they are right-justified in which case high-order (leftmost character) truncation occurs. For numeric data, high-order truncation occurs to left of the decimal point and low-order to right of decimal point. b) If FIELD-A PIC 999V999 VALUE 123.456 and FIELD-B PIC 99V99, then MOVE FIELD-A TO FIELD-B results in FIELD-B with value of 23.45.
14. Only thing I'd add to Larry's answer is that rounding of the result occurs last.

Comment by Larry on 2007-09-28 Rate this Comment

1. They all look invalid to me - but this one would be best checked.
2. Where do those numbers come from, I have no idea what adding two variables together who's value I don't know would be.
3. ??
4. D
5. Maybe C, good one to test
6.I think C, but that may be a Java answer
7. B looks best
8. maybe false, where's the negative?
9. 1,7,6,3,4,5,2 (also should test)
10. -, maybe something else
11. not at all sure
12. not at all sure
13. that has something to do with the beginning or end of the field being cut off I think
14. (B + C) , / 5, + --> parnes first, then divide, then add

Comment by archive on 2007-09-28 Rate this Comment

Hi - can you help me - you seem to be quite a genious in COBOL. I have 13 questions - for you you might be able to answer these just by looking at it. These are for an assignment that is due on monday - so I would greatly appreciate it if you can help me on this. I'm just learning COBOL but I'm good in other programming languages if you require my help some day. I need to be able to pass these assignments as I need to get a good paying job when I pass the course.

QUESTION 1

1. Which of the following is (are) not a valid arithmetic statement in COBOL?

a) ADD 5 FIELD-1 FIELD-2 AND FIELD-3 TO FIELD-4 ROUNDED.

b) SUBTRACT A-FIELD ROUNDED FROM B-FIELD GIVING C-FIELD

c) DIVIDE A-FIELD BY B-FIELD ROUNDED REMAINDER C-FIELD

d) DIVIDE A-FIELD BY B-FIELD GIVING C-FIELD END DIVIDE.

e) ALL ARE INVALID.

======================

QUESTION 2

The result of the operatin ADD FIELD-B to FIELD-A will be

a) 5

b) -5

c) 15

d) -15

e) not possible, as FIELD-A is not signed.

=================

question 3

an in-line PERFORM

a) can never be terminated with an END-PERFORM.

b) must be terminated with a period.

c) transfers control to the paragraph stated

d) can contain any number of statement

e) none of the above

-----------------------------------------

question 4

Given the following recod description:

01 TRANSACTION-RECORD.

05 TR-NAME PIC X(15).

05 TR-STATUS PIC X.

88 VALID-STATUS VALUES "A", "B", "C", "D".

88 MARRIED VALUE "A".

88 SINGLE VALUE "B".

88 SEPARATED VALUE "C".

88 DIVORCED VALUE "D".

05 TR-NET-WORTH PIC 9(8).

All of the following entries are equivalent except:

a) IF VALID-STATUS

b) IF TR-STATUS = "A" OR "B" OR "C" OR "D"

c) IF MARRIED OR SINGLE OR SEPARATED OR DIVORCED

d) IF MARRIED AND SINGLE AND SEPARATED AND DIVORCED

---------------------

question 5

A PERFORM VARYING loop is to be coded to loop exactly 5 times. The YEARS-WORKED field is to be added to WORK-TOTALS. Assuming WORK-TOTALS has been initialized to zero, which is the correct code?

a) PERFORM VARYING COUNT FROM 1 BY 1 UNTIL > 5

ADD YEAR-WORKED TO WORK-TOTALS

END-PERFORM

b) PERFORM VARYING COUNT FROM 1 BY 1

UNTIL COUNT > 5

ADD YEARS-WORKED TO WORK-TOTALS

END-PERFORM.

c) PERFORM VARYING COUNT FROM 1 BY 1

UNTIL WORK-INDEX = 5 WITH TEST AFTER

ADD YEARS-WORKED TO WORK-TOTALS

END-PERFORM.

d) PERFORM VARYING COUNT FROM 1 BY 1

UNTIL COUNT LESS THAN 6

ADD YEARS-WORKED TO WORK-TOTALS

END-PERFORM.

e) PERFORM VARYING COUNT FROM 1 BY 1

WITH TEST AFTER UNTIL COUNT = 5

ADD YEARS-WORKED TO WORK-TOTALS

END-PEFORM.

=============

question 6

Removal of parantheses would affect all of the following except:

a) COMPUTE X = (A + B)/(C+D)

b) COMPUTE X = (A+B)/C

c) COMPUTE X = A + (B**2)/C

d) COMPUTE X = (A + B)/(C**2)

=====================

question 7

Assuming that all receiving fields have been suitably defined and initialized to spaced and that FIELD-A is defined as PIC X(25) VALUE "BILLY*.S., BARNES", what is the contents of FIELD-C after the statement UNSTRING FIELD-A DELIMITED BY ",", "&", "#" OR ALL SPACES INTO FIELD-B, FIELD-C AND FIELD-D.

a) BILLY

b) S.

c) S

d) BARNES

==================

question 8

a NUMERIC FIELD OF S9(6)V99 having a current value of -435.50 with an edit patter of ---,---.99DB produces an edited result of =$435.50DB

a) True

b) False

=========

question 9

When completing the logic portion of a flowchart within 200-PROCESS-RTN, what is the correct sequence of operations? Place numbers from 1 (first step) through to 6 (last step) beside the operation at the left.

Read input record

ADD 1 to record written

Write output record

Reject input record if meets condition for rejection

Create new output field if required

Move input fields to output record

Add 1 to records read count

================

question 10

What are the different ways that a negative amount can be indicated on an output line? (Show the edit patterns).

=========

question 11

List the figurative constants.

=========

question 12

What are the insertion characters?

=========

question 13

a) What is the difference between high and low order truncation?

b) GIVEN FIELD-A and FIELD-B, provide definitions (i.e. PICTURE clauses) of these 2 fields and a corresponding MOVE statement that demonstrates both high and low-order truncation.

========

question 14

What is the sequence of events when COBOL carries out this statement:

COMPUTEe FIELD-A ROUNDED = (B + C) / 5 + FIELD-N

======

Comment by TheMadProfessor on 2013-05-22 Rate this Comment

A reverse-text solution (this uses fixed-length fields, where nn signifies the text field lengths – creating a subroutine that handles varying lengths is a far messier problem.)

....77 TEXT-LENGTH PIC S9(4) COMP VALUE nn.
....77 SUB1 PIC S9(4) COMP.
....77 SUB2 PIC S9(4) COMP.

LINKAGE SECTION.
....01 ORIGINAL-TEXT PIC X(nn).
....01 REVERSED-TEXT PIC X(nn).

PROCEDURE DIVISION USING ORIGINAL-TEXT, REVERSED-TEXT.

....MOVE SPACES TO REVERSED-TEXT.
....MOVE +1 TO SUB2.

....PERFORM VARYING SUB1 FROM TEXT-LENGTH BY -1
......UNTIL SUB1 LESS THAN 1
.........OR ORIGINAL-TEXT (SUB1:1) NOT EQUAL SPACE
....END-PERFORM.

....PERFORM VARYING SUB1 BY -1
......UNTIL SUB1 LESS THAN 1
........MOVE ORIGINAL-TEXT(SUB1:1) TO REVERSED-TEXT(SUB2:1)
........ADD +1 TO SUB2
....END-PERFORM.

....GOBACK.

(This assumes that trailing spaces in the original text do not become leading spaces in the reversed text. If not, the two PERFORMs above are replaced by the one below.)

....PERFORM VARYING SUB1 FROM TEXT-LENGTH BY -1
.......UNTIL SUB1 LESS THAN 1
........MOVE ORIGINAL-TEXT(SUB1:1) TO REVERSED-TEXT(SUB2:1)
........ADD +1 TO SUB2
....END-PERFORM.

Comment by TheMadProfessor on 2007-10-11 Rate this Comment

One employee report solution:

77 SUB1 PIC S9(4) COMP.
77 CALC-AVERAGE PIC S9(8) COMP.

01 TOTALS-TABLE.
....05 PIC X(7) VALUE ‘MEN.
....05 PIC S9(4) COMP VALUE +0.
....05 PIC S9(8) COMP VALUE +0.
....05 PIC X(7) VALUE ‘WOMEN’.
....05 PIC S9(4) COMP VALUE +0.
....05 PIC S9(8) COMP VALUE +0.
....05 PIC X(7) VALUE ‘UNKNOWN’.
....05 PIC S9(4) COMP VALUE +0.
....05 PIC S9(8) COMP VALUE +0.
....05 PIC X(7) VALUE ‘TOTAL’.
....05 PIC S9(4) COMP VALUE +0.
....05 PIC S9(8) COMP VALUE +0.
01 FILLER REDEFINES TOTALS-TABLE.
....05 FILLER OCCUR 4 TIMES.
........10 TAB-ID PIC X(7).
........10 TAB-COUNT PIC S9(4) COMP.
........10 TAB-ACCUM PIC S9(8) COMP.

(In file read loop)
EVALUATE GENDER-CODE
....WHEN ‘M’
........MOVE +1 TO SUB1
........MOVE SALARY TO salary-display-field
....WHEN ‘F’
........MOVE +2 TO SUB1
........MOVE SALARY TO salary-display-field
....WHEN OTHER
........MOVE +3 TO SUB1
........MOVE ALL ‘*’ TO salary-display-field
END-EVALUATE.

ADD +1 TO TAB-COUNT(SUB1), TAB-COUNT(4).
ADD SALARY TO TAB-ACCUM(SUB1), TAB-ACCUM(4).
Move name fields and write detail report line.

(After all data read and processed)
PERFORM VARYING SUB1 FROM 1 BY 1
....UNTIL SUB1 GREATER THAN 4
.....DIVIDE TAB-ACCUM(SUB1) BY TAB-COUNT(SUB1)
........GIVING CALC-AVERAGE ROUNDED
........ON SIZE ERROR MOVE ZERO TO CALC-AVERAGE
.....END-DIVIDE
Move and write summary line using TAB-ID(SUB1), TAB-ACCUM(SUB1) and CALC-AVERAGE
END-PERFORM.

Comment by archive on 2007-08-16 Rate this Comment

ive managed to do few other simple programs and ive succeeded in doing it. ive got this program from a very old book and ive done the program but with a lot of errors. im doing my business studies and recently ive been keen on programming. my friends advised me to start from cobol to java, visual basic and c. i would really appreciate if you could help me from cracking my head. i need an example program of this form to do so. thanks alot

The main function of this program is to read records from an employee file and produce a report such as below:

Employee record structure:
columns 1-15 employee name
columns 16 gender code
columns 17-23 annual salary

If there is an error and a recored does not inclide a valid gender code, then asterisks are printed in the report to highlight error. The occurrence of such an error is illustrated in the sample report for the case of the emplouee named Proust,K

Sample Salary Report:


ANNUAL SALARY

EMPLOYEE NAME MEN WOMEN

Jones,A 28,200
Anderson,P 22,000
Proust,K ********
Maggi,M 30,000

TOTAL $50,200 $30,000

AVERAGE $25,100 $30,000


p/s: im a beginner and im really confuse. Could you please help me with this question. Hope to hear from you soon. Thank you very much

Comment by archive on 2007-08-16 Rate this Comment

Forgive me disturbing you but I read your words on the SAMS Teach Yourself COBOL book.

I wonder if you would be kind enough to let me know what the ACCEPT statement allows. Does it allow, for instance,

01 DATANAME PIC X(10).

DISPLAY "Name:" AT 1021.
DISPLAY DATANAME AT 1040.

to display some data at a particular coordinate upon a screen

Similarly can you say

ACCEPT DATANAME AT 1040.
DISPLAY DATANAME AT 1240.

where one is allowing the user to key in data at that coordinate? And then displaying that resulting 'update'.

Thanks for taking the trouble to read this.

Comment by archive on 2007-08-16 Rate this Comment

hi ..
can u answer me this question in cobol.
q1: write program to output reverse text which i input
such as : i like tea
out put : aet ekil i

q2: write program to input information about five
student
witch is to every student
name/mark1/mark2/mark3
and output such as
name average
==== =======

q3:write program to input information about five
student which is to every student
name / sex "m or f"
and output such as
number of female is :
number of male :


please i need it . with all wishes.

 
Sign in to comment on COBOL Comments and Questions.