Data Item Type Help - Not Displaying Expected Value



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Data Item Type Help - Not Displaying Expected Value

Postby Cuong_Daor » Mon May 18, 2020 3:23 pm

Hi Guys,

I am new to the forum, obviously (and in dire hehe) need of help regarding my COBOL program.
Been racking my brain over this and can't seem to figure out the issue.

My Program is below (left out opening and closing of files and few other unnecessary bits).

       IDENTIFICATION DIVISION.
       PROGRAM-ID. TBLE0503.
      *Aim - Practicing using INDEX with tables

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT REC-PRICE-PART-FILE ASSIGN TO "REC-PRICE-PART.DAT"
         FILE STATUS IS REC-PRICE-PART-FILE-STATUS
              ORGANIZATION IS LINE SEQUENTIAL.
         
               
       DATA DIVISION.
       FILE SECTION.
       FD REC-PRICE-PART-FILE.
       01 REC-PRICE-PART.                
            05 REC-ITEM-NUMBER        PIC 9(3).
            05 REC-PRICE              PIC S99V99.
       
   
       WORKING-STORAGE SECTION.
       01 FILE-STATUS.
          05 REC-PRICE-PART-FILE-STATUS  PIC X(2).
                               
       01 PRICE-TABLE               VALUE ZERO.
          05 PRICE-GROUP            OCCURS 16 TIMES
                                    INDEXED BY PRICE-GRP-INDEX.
             10 ITEM-NUMBER         PIC 9(3).
             10 ITEM-PRICE          PIC 9(2)V9(3).
           
       01 SWITCHES.
          05 PRICE-TABLE-EOF-SWITCH   PIC X VALUE 'N'.
             88 PRICE-TABLE-EOF             VALUE 'Y'.

       01 WS-INPUT.
          05 INP-ITEM-NUMBER        PIC 9(3).
          05 INP-ITEM-PRICE         PIC 9(2)V9(3).

PROCEDURE DIVISION.
       2000-MAIN-PROGRAM.
          PERFORM 9100-OPEN-FILE
          PERFORM 3500-LOAD-TABLE
          PERFORM 9110-CLOSE-FILE
         
          PERFORM 5000-MODIFY-TABLE
          PERFORM 9000-STOP-PROGRAM
       .

   
       3500-LOAD-TABLE.
          PERFORM 9200-READ-RECORD
          PERFORM 3510-LOAD-LVL-1-TABLE
             WITH TEST AFTER
             VARYING PRICE-GRP-INDEX FROM 1 BY 1
             UNTIL PRICE-GRP-INDEX = 16
                OR PRICE-TABLE-EOF
       .

       
       3510-LOAD-LVL-1-TABLE.
          IF NOT PRICE-TABLE-EOF
             MOVE REC-ITEM-NUMBER TO ITEM-NUMBER (PRICE-GRP-INDEX)
             MOVE REC-PRICE TO ITEM-PRICE (PRICE-GRP-INDEX)
          END-IF
          PERFORM 9200-READ-RECORD
       .

       5000-MODIFY-TABLE.
          PERFORM
          WITH TEST AFTER
          UNTIL INP-ITEM-NUMBER = 999
          DISPLAY 'Enter Item #' WITH NO ADVANCING
          ACCEPT INP-ITEM-NUMBER
          SET PRICE-GRP-INDEX TO 1
          SEARCH PRICE-GROUP VARYING PRICE-GRP-INDEX
          AT END
             DISPLAY 'Nothing Found'         

          WHEN ITEM-NUMBER (PRICE-GRP-INDEX) = INP-ITEM-NUMBER
             DISPLAY 'Current Price ' ITEM-PRICE (PRICE-GRP-INDEX)
             DISPLAY 'Enter New Price'
             ACCEPT INP-ITEM-PRICE
             SET ITEM-PRICE (PRICE-GRP-INDEX) TO INP-ITEM-PRICE
             DISPLAY 'New Price ' ITEM-PRICE (PRICE-GRP-INDEX)           
          END-SEARCH
          END-PERFORM     
       .

 


The program is reading data in from a simple file that contains:
1011250
1075000
1110770
1580555
1616250

After that I am searching through the table and updating the item price if the user input matches an item number in the table.

Below is the program running:

Enter Item #101
Current Price 12.500
Enter New Price
.29
New Price 00.290 Expected
Enter Item #101
Current Price 00.290
Enter New Price
.293
New Price 00.293 Expected
Enter Item #101
Current Price 00.293
Enter New Price
22.23
New Price 22.230 Expected
Enter Item #101
Current Price 22.230
Enter New Price
23.236
New Price 23.230 UNEXPECTED - Why is the 6 truncated
Enter Item #101
Current Price 23.230
Enter New Price
3.236
New Price 03.236 EXPECTED - So it is correct only if I have 1 digit to the left of the decimal?
Enter Item #101
Current Price 03.236
Enter New Price
23.556
New Price 23.550 UNEXPECTED - Why is the 6 truncated
Enter Item #999
Nothing Found


So I can sort of see what is happening, I just don't understand why. I have defined my 'INP-ITEM-PRICE' data name to have enough storage for up to 99.999 I believe....
My Accept statement seems to be ignoring the last decimal value (if input a value > 9)?

Anyone got any idea where my understanding is incorrect? Or where my code is incorrect? But I believe it's not my code, but rather my understanding that is the issue here.

BTW, I am building/compiling on windows 10, but using Cygwin emulator thingy, and I am pretty sure I am using the COBOL-85 standard with the 1989 intrinsic functions added.

Thanks Guys, first post so I hope I've correctly posted.
Cuong_Daor
 
Posts: 3
Joined: Mon May 18, 2020 2:42 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Data Item Type Help - Not Displaying Expected Value

Postby NicC » Mon May 18, 2020 3:44 pm

I hope I've correctly posted

Apart from the fact that this is a mainframe forum not PC/Unix/windows it is not too bad a post.
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times

Re: Data Item Type Help - Not Displaying Expected Value

Postby Robert Sample » Mon May 18, 2020 6:11 pm

You've defined INP-ITEM-PRICE as PIC 9(2)V9(3), then you ACCEPT values for it. The V in a PICTURE clause means the decimal point is IMPLIED -- as in, the decimal point does not really exist. So you have defined a 5-byte variable, not a 6-byte variable. Attempting to provide a value of 23.236 (or any 6-character value) for this variable means that only the first 5 characters will be used -- hence the 6th character will be ignored. And yes, the variable can have a value of 99.999 -- but the decimal point is not really there, so the value actually is 99999.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Data Item Type Help - Not Displaying Expected Value

Postby Cuong_Daor » Mon May 18, 2020 7:51 pm

Wait, so the decimal is taking up a byte?
So basically, I need to take into consideration a decimal (implied) will take up a byte?

But now I am confused because if I modify the program a bit so that I only read from my sequential text file into my tables, and then I just display the values I don't have this issue of 5-byte/6-byte variable...

For example, I add a paragraph to display the values in my table.

PROCEDURE DIVISION.
          PERFORM 4000-DISPLAY-REPORT
     *    PERFORM 5000-MODIFY-TABLE

       4000-DISPLAY-REPORT.
          DISPLAY '4000-DISPLAY-REPORT'
          PERFORM
          WITH TEST AFTER
          VARYING PRICE-GRP-INDEX FROM 1 BY 1
          UNTIL PRICE-GRP-INDEX = 16
          DISPLAY 'ITEM # ' ITEM-NUMBER (PRICE-GRP-INDEX)
                  ' ITEM PRICE ' ITEM-PRICE (PRICE-GRP-INDEX)
          END-PERFORM        
       .
 


I am still loading in my values from my text file. So if I do this, the output is as follows which is correct/perfect.

ITEM # 101 ITEM PRICE 12.50
ITEM # 107 ITEM PRICE 50.00
ITEM # 111 ITEM PRICE 07.70
ITEM # 158 ITEM PRICE 05.55


The variable I have defined in my FILE SECTION

05 REC-PRICE              PIC S99V99.
 

So that will enough for 4 bytes yeh, which is fine cause the values are 4 digits in my file anyways.
But if I need to take into consideration the implied decimal, that means I would need a 5-byte variable (otherwise I would get this truncation issue right)? But I don't have this issue as you can see from the output above.

So this is where I am confused a bit.

If I read the values in from the file into my table and then just display it, no issue.

But if after I read the values in from the file into my table, and then if I would to update with a user input... I run into this issue.

I don't understand why it is an issue in one scenario and not in the other?
Am I not understanding it correctly here???
Cuong_Daor
 
Posts: 3
Joined: Mon May 18, 2020 2:42 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Data Item Type Help - Not Displaying Expected Value

Postby Robert Sample » Mon May 18, 2020 9:13 pm

So basically, I need to take into consideration a decimal (implied) will take up a byte?
This is the exact opposite of reality -- the implied decimal does not, under any circumstances, take up a byte: implied means implied. If you used PIC 9(2).9(3), then the decimal point takes a byte. The issue that you posted about is that you had 5 decimal digits and a decimal point (which is 6 characters) in your input -- but your variable only had 5 characters available to store those six characters. ACCEPT is a notoriously bad input method since the data your program gets may (as you have seen) not reflect the input data.

Furthermore, if your data is displaying as you've shown (with decimal points) but your PICTURE clause uses an implied decimal point, then you need to closely and carefully read the COBOL manual for your version of COBOL. In general, variables with implied decimal points will only display decimal points IF THEY ARE LOADED AS PART OF THE DATA. In other words, you are violating the rules of COBOL (by providing decimal points as input to variable that have implied decimal points) and confused about the results you're getting. Some COBOL compilers may handle implicit decimal points differently than the mainframe does; this is why I think you need to read the language reference manual for your COBOL very carefully.

Try moving your variable to a packed decimal (COMP-3) variable -- on the mainframe, you'll get a DATA EXCEPTION (S0C7 abend) due to the decimal point (X'4B' in EBCDIC, which is not a character that can be packed).
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Data Item Type Help - Not Displaying Expected Value

Postby Cuong_Daor » Thu May 21, 2020 3:58 am

Thanks Robert,

I will have to do some further reading on this subject, as I am still trying to wrap my head around the theory.
Cuong_Daor
 
Posts: 3
Joined: Mon May 18, 2020 2:42 pm
Has thanked: 0 time
Been thanked: 0 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post