Search code examples
cobol

On Size Error doesn't give expected output


When I run the below code I get the following output:

       Var1: 761758
Actual-Var1: 761.758
Result-Var1: 761.75

What I'm expecting is:

Result-Var1 is incorrect size!
       Var1: 761758
Actual-Var1: 761.758
Result-Var1: 761.75

I'm teaching myself COBOL and I'm using Michael Coughlan's book, Beginning COBOL for Programmers. His minimal example of the ON SIZE ERROR phrase is on page 62. It seems straight forward but I can't get the output that I want. Any suggestions? In addition, I'm using Micro Focus Visual COBOL for Ecplise as my IDE.

       Identification Division.
       Program-ID. OnSizeErrorDemo.

       Data Division.
       Working-Storage Section.
         01 Var1 Pic 999V999 Value 761.758.
         01 Actual-Var1 Pic 999.999.
         01 Result-Var1 Pic 999.99.

       Procedure Division.
       Begin.
         Move Var1 To Actual-Var1

         Compute Result-Var1 = Var1
           On Size Error Display "Result-Var1 is incorrect size!"
         End-Compute

         Display "       Var1: ", Var1
         Display "Actual-Var1: ", Actual-Var1
         Display "Result-Var1: ", Result-Var1
       Stop Run

Solution

  • The result appears to be correct. Low-order digits will be truncated unless the ROUNDED phrase is used.

    The SIZE ERROR phrase would take affect if the value of the result is greater than the PICTURE clause, in this case 999.99. Try

     Compute Result-Var1 = Var1 + Var1
       On Size Error Display "Result-Var1 is incorrect size!"
     End-Compute
    

    to test the SIZE ERROR phrase. Result-Var1 should be unchanged.