Page 1 of 1

Check if input value is type INTEGER?

PostPosted: Sat Dec 21, 2013 3:34 am
by xboss
Learning COBOL Programming 101.

Below is my program. I am trying to determine if the value user keyed in is NUMERIC or not. BALANCE-INPUT that accepts user input is defined as type VARCHAR. Irrespective of the type of input I key in, same result "INVALID BALANCE. SHOULD BE NUMERIC VALUE".

       IDENTIFICATION DIVISION.                                         
      *--------------------                                             
       PROGRAM-ID. INTCHECK.                                           
      *--------------------                                             
       ENVIRONMENT DIVISION.                                           
      *--------------------                                             
       DATA DIVISION.                                                   
      *-------------                                                   
       WORKING-STORAGE SECTION.                                         
      *-----------------------                                                                 
       01 ACCEPT-INPUT.                                                 
          02 BALANCE-INPUT      PIC X(10).                             
          02 AMOUNT-INPUT       PIC S9(5)V99 USAGE IS COMP-3.           
                                                                       
       PROCEDURE DIVISION.                                             
      *------------------                                               
       BEGIN.                                                           
           DISPLAY 'ENTER AMOUNT TO DEPOSIT'                           
           ACCEPT BALANCE-INPUT                                         
           STRING BALANCE-INPUT DELIMITED BY SPACES INTO BALANCE-INPUT 
           IF BALANCE-INPUT IS NUMERIC                                   
                 COMPUTE AMOUNT-INPUT = FUNCTION NUMVAL(BALANCE-INPUT)   
                 DISPLAY 'IS NUMERIC'
       DISPLAY AMOUNT-INPUT                                   
           ELSE                                                         
                 DISPLAY 'INVALID BALANCE. SHOULD BE NUMERIC VALUE'   
       DISPLAY BALANCE-INPUT                                   
           END-IF.                                                       
           GOBACK.

Re: Check if input value is type INTEGER?

PostPosted: Sat Dec 21, 2013 3:49 am
by Terry Heinze
I believe the ACCEPT verb as written defaults to whatever you have following the SYSIN DD statement, so the user is not being given the chance to key in anything. See the ACCEPT verb in the Language Reference Manual. Please post your JCL.

Re: Check if input value is type INTEGER?

PostPosted: Sat Dec 21, 2013 3:52 am
by Terry Heinze
Also, VARCHAR is a DB2 term, not COBOL. BALANCE-INPUT is a 10-byte elementary item.

Re: Check if input value is type INTEGER?

PostPosted: Sat Dec 21, 2013 3:59 am
by xboss
Terry,
This is interactive program, my input is coming from user as below. Sorry I should have used term "alphanumeric" instead of "Varchar".

 ENTER AMOUNT TO DEPOSIT                 
100                                     
 INVALID BALANCE. SHOULD BE NUMERIC VALUE

Re: Check if input value is type INTEGER?

PostPosted: Sat Dec 21, 2013 4:30 am
by Robert Sample
COBOL tends to be picky about variables. When you code BALANCE-INPUT as PIC X(10), every input line you provide needs to be 10 bytes (0000000100 for your sample instead of 100) or your BALANCE-INPUT variable will not be numeric. One way this can be handled is by using intrinsic function NUMVAL to convert the data to a numeric value.

Re: Check if input value is type INTEGER?

PostPosted: Sat Dec 21, 2013 5:59 am
by BillyBoyo
Except if non-number-format data is typed, NUMVAL will abend.