Reputation: 919
I am having a bit of trouble with my COBOL homework. I have to make a program that writes out the names of people and their social security numbers. Basically I have toy make a number like 123456789 show up like 123-45-6789 and a name like JSDOE show up like J S DOE. Can someone help me out?
Upvotes: 2
Views: 4013
Reputation: 4417
A B
in the PICTURE
of the receiving data-item will insert a space during a MOVE
. For the name, a MOVE
is all that is required. For the number, the spaces may be replaced with hyphens.
data division.
working-storage section.
1 num-in pic 9(9) value 123456789.
1 num-out pic 999B99B9999.
1 name-in pic x(5) value "JSDOE".
1 name-out pic XBXBXXX.
procedure division.
move num-in to num-out
inspect num-out replacing all space by "-"
move name-in to name-out
display num-out
display name-out
stop run
.
Output:
123-45-6789
J S DOE
Upvotes: 0
Reputation: 61046
You should do something like.
01 toyNumber pic 9(9).
01 yourNumber.
03 a pic x(3).
03 b pic x(2).
03 c pic x(4).
01 outNumber.
03 a1 pic x(3).
03 filler pic x value "-".
03 b1 pic x(2).
03 filler pic x value "-".
03 c1 pic x(4).
and in the procedure:
move 123456789 to toyNumber.
....
move toyNumber to yourNumber.
move a to a1.
move b to b1.
move c to c1.
display outNumber.
Or you may use "move corresponding" if you are allowed in your homework.
Hope this help!
PS: The trick for the name is the same ...
Upvotes: 7
Reputation: 27478
A more modern (less ancient?) approach :-
STRING SSNUMBER(1:3) DELIMITED BY SIZE
'-' DELIMITED BY SIZE
SSNUMBER(4:5) DELIMITED BY SIZE
'-' DELIMITED BY SIZE
SSNUMBER(6:9) DELIMITED BY SIZE
INTO PRINTFIELD.
Upvotes: 2
Reputation: 2579
COBOL!!
I am writing this after a long time. So, apply caution. Something like this may work:
01 SSN.
03 SSN-FIRST PIC X(03) VALUE SPACES.
03 SSN-FDASH PIC X VALUE "-".
03 SSN-MIDDLE PIC X(02) VALUE SPACES.
03 SSN-MDASH PIC X VALUE "-".
03 SSN-LAST PIC X(04) VALUE SPACES.
01 NAME.
03 FNAME PIC X(10) VALUE SPACES.
03 FDASH PIC X VALUE SPACES.
03 FMIDDLE PIC X(10) VALUE SPACES.
03 MDASH PIC X VALUE SPACES.
03 FLAST PIC X(10) VALUE SPACES.
Upvotes: 2