assumed decimal as input - tab and enter add trailing zeros

Viewed 108

Good evening,

I just started learning Cobol, and to practice, I wanted to program a simple multiplication program, working with two decimal numbers given by the user.

So I wrote the following

   IDENTIFICATION DIVISION.
   PROGRAM-ID. exo.

   DATA DIVISION.
   WORKING-STORAGE SECTION.
   77 a PIC 9(2)V9.
   77 b PIC 9(2)V9.
   77 result PIC 9(4)V99.
   screen section.
   1 pla-title.
          2 blank screen.
          2 line 1 col 1 value 'Multiplication'.
   1 pls-numbers.
          2 line 4 col 2 value 'Number 1 : '.
          2 PIC 9(2)V9 to a required.
          2 line 5 col 2 value 'Number 2 : '.
          2 PIC 9(2)V9 to b required.
   1 pla-result.
          2 line 7 col 2 PIC 9(2)V9 from a.
          2 col 7 value 'x'.
          2 col 9 PIC 9(2)V9 from b.
          2 col 13 value '='.
          2 col 15 PIC 9(4)V99 from result.

   PROCEDURE DIVISION.
   display pla-title.
   accept pls-numbers.
   compute result = a * b.
   display pla-result.

   END PROGRAM exo.

The issue here is not the multiplication part, which works perfectly. The problem is the input. Every time I press tab or enter, there is a 0 added at the end. For instance, typing 9,9,9,tab,9,9,9,enter multiplies 900 with 990, since the format only takes the last three digits of 99900 (tab+enter) and 9990 (enter).

I tried the same program with integers, it works perfectly. I tried the same with real decimals too (9(2).9), and the input works, but I have a problem with the multiplication (not a numeric value), that I will try to understand later, one problem at the time.

So in short, the question is to know why the input is modified by typing tab and enter, that in my understanding is used to navigate the accept fields.

Thank you very much for your help!

1 Answers

How the input is automatically adjusted on field change/finish of the accept depends on the compiler/runtime actually in use (it is good in most times to add this information in the question), but most COBOL variants want a "." (or in the case of DECIMAL-POINT IS COMMA a ",") entered to get the decimal part correct.

Actually the results with an implied decimal-point and ACCEPT may not be what you want it to do. I suggest to try using a numeric-edited field like ZZ9.99 (which is auto-de-edited on MOVE to a field you do the calculation with [if the stored data matches the editing symbols] after the ACCEPT) or a plain PIC X and a MOVE FUNCTION NUMVAL (input-field) TO a afterwards (this should work on any compiler including this function).

Related