1

I'm new to COBOL and our latest training activity is we will try to get an output of three items like this:

  1. 0000/2013
  2. 00012345
  3. 12345**

I have tried with my code below but it clearly does not give me the output I want. It does not display zeroes.

IDENTIFICATION DIVISION.

PROGRAM-ID. ACTIVITY4.

ENVIRONMENT DIVISION.

DATA DIVISION.

WORKING-STORAGE SECTION.

01 NUM1 PIC z(4)9(4) VALUE 2013.
01 NUM2 PIC Z(3)9(5) VALUE 12345.
01 NUM3 PIC 9(5) VALUE 12345.
01 E-NUM1 PIC *(2)9 VALUE 0.

PROCEDURE DIVISION.

DISPLAY-VALUES.

DISPLAY-ZEROES.

MOVE E-NUM1 TO NUM3.

 DISPLAY "1) "NUM1.

 DISPLAY "2) "NUM2.

 DISPLAY "3) "NUM3.

 STOP RUN.
Bill Woodger
  • 12,968
  • 4
  • 38
  • 47

3 Answers3

2

It looks to me like Activity 4 is about formatting data for output. You need to produce:

0000/2013
00012345
12345**

The first requires the insertion of a slash. COBOL has a slash insertion symbol, '/'. The second is to ensure no zero-suppression, which is the behaviour when a PIC 9(n) field is output, so nothing really to do other than get the length right. The third apparently draws you to the "*" replacement edit field, which is the intention of the question I guess, but the * editing symbol replaces leading zeros only.

One simple way to get the trailing asterisks is to use the data-definition. Define a group item, which is what you will DISPLAY. Subordinate to that, define your number (PIC 9(5)) and follow it immediately with a FILLER (named field if you like) PIC XX which has a VALUE of "**" (or ALL "*", a little extreme for a two-byte field though).

IDENTIFICATION DIVISION.

PROGRAM-ID. ACTIVITY4.

ENVIRONMENT DIVISION.

DATA DIVISION.

WORKING-STORAGE SECTION.

01 NUM1 PIC 9(4)/9(4).
01 NUM2 PIC 9(8).
01 NUM3-OUT. 
    05  NUM3 PIC 9(5). 
    05  FILLER PIC XX VALUE "**".

PROCEDURE DIVISION.

DISPLAY-VALUES.

DISPLAY-ZEROES.
 MOVE 1234 TO NUM1

 DISPLAY "1) " 
             ">"
             NUM1
             "<"
 MOVE 12345 TO NUM2

 DISPLAY "2) "
             ">"
             NUM2
             "<"
 MOVE 12345 TO NUM3
 DISPLAY "3) "
             ">"
             NUM3-OUT
             "<"

 STOP RUN
 .

I have never put a VALUE on an edited field. The editing is carried out when the field is a target of a COBOL verb. In the COBOLs I use this would not effect the edit, it would just have that literal value. I don't know about GNU OpenCOBOL (formerly OpenCOBOL).

Bill Woodger
  • 12,968
  • 4
  • 38
  • 47
0

Drop the Z when declaring the numbers.

Z suppresses leading zeros in a number in COBOL.

Simply changing your variable declarations to:

01 NUM1 PIC 9(8) VALUE 00002013.
01 NUM2 PIC 9(8) VALUE 00012345.

(You don't actually need the preceding zeroes in the number, they're just there to show you the whole PIC.)

Please see This for a more detailed description on picture statements and a quick summary of the different character modifiers and what they do.

I didn't catch the other issue at first, however the other variable you created should be

01 E-NUM1 PIC 9(2) VALUE 0.
Happington
  • 454
  • 2
  • 8
  • it still does not work i have tried every compile option still the same – Karl laurenz Bagasan Oct 21 '13 at 06:51
  • The only other change to your code I had to make was to the third variable declaration, (Where you declare *(2)9). I'll update my answer to reflect this. You should be using the -x and -free (-free is optional, but nice) arguments when compiling. Also, you say you want your output for #3 to be 12345, but you move a 0 into the variable where you hold 12345, was this just to play with things? – Happington Oct 21 '13 at 07:02
0

Regarding the 12345** issue: If nothing else works, you can concatenate the number to the asterisks with a STRING like this:

WORKING-STORAGE SECTION.

01 NUM3 PIC X(7).
01 NUM3-NUMBERS PIC 9(5) VALUE 12345.

PROCEDURE DIVISION.

STRING NUM3-NUMBERS "**" DELIMITED BY SIZE INTO NUM3.
DISPLAY "3) "NUM3.

What this code does is adding 2 asterisks at the end of your number. (but your NUM3 is now an alphanumeric picture instead a numeric one).

Another thing i just found in an old cobol book here is the * Check Protection Character. I have never tried it but here it states it should be used this way:

01 NUM3 PIC 9(5)*(2).

Give it a try!

Molusco
  • 59
  • 6
  • The book may say that the check-protection only works on leading zeros. Mine does. I doubt any COBOL compiler performs suppression of trailing zeros, even if the value had trailing zeros - which it doesn't. What do you feel would be the advantage of using STRING over just having it sitting there all the time in WORKING-STORAGE? – Bill Woodger Oct 22 '13 at 21:23