Check if input value is type INTEGER?



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

Check if input value is type INTEGER?

Postby xboss » Sat Dec 21, 2013 3:34 am

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.
xboss
 
Posts: 79
Joined: Mon Nov 29, 2010 10:55 am
Has thanked: 0 time
Been thanked: 0 time

Re: Check if input value is type INTEGER?

Postby Terry Heinze » Sat Dec 21, 2013 3:49 am

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.
.... Terry
Terry Heinze
 
Posts: 239
Joined: Wed Dec 04, 2013 11:08 pm
Location: Richfield, MN, USA
Has thanked: 12 times
Been thanked: 11 times

Re: Check if input value is type INTEGER?

Postby Terry Heinze » Sat Dec 21, 2013 3:52 am

Also, VARCHAR is a DB2 term, not COBOL. BALANCE-INPUT is a 10-byte elementary item.
.... Terry
Terry Heinze
 
Posts: 239
Joined: Wed Dec 04, 2013 11:08 pm
Location: Richfield, MN, USA
Has thanked: 12 times
Been thanked: 11 times

Re: Check if input value is type INTEGER?

Postby xboss » Sat Dec 21, 2013 3:59 am

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
xboss
 
Posts: 79
Joined: Mon Nov 29, 2010 10:55 am
Has thanked: 0 time
Been thanked: 0 time

Re: Check if input value is type INTEGER?

Postby Robert Sample » Sat Dec 21, 2013 4:30 am

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.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Check if input value is type INTEGER?

Postby BillyBoyo » Sat Dec 21, 2013 5:59 am

Except if non-number-format data is typed, NUMVAL will abend.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post