IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How to accept only NUMERIC DATA in COBOL


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
hello2satish

New User


Joined: 21 Jul 2007
Posts: 5
Location: Mumbai

PostPosted: Tue Aug 14, 2007 5:49 pm
Reply with quote

IN THE FOLLOWING PROGRAMM THERE IS VALIDATION THAT WS-ACCNO SHOULD ACCEPT ONLY NUMERIC VALUE (1,2....9.0) NO ALPHANUMERIC VALUES ARE ALLOWED BUT IT ACCEPT ALPHANUMERIC ALSO.

I WANT TO ACCEPT ONLY NUMERIC VALUE PLEASE HELP.

WHAT MODIFICATION DO I NEED TO DO IN FOLLOWING PROGRAMM ?

2000-MAIN.
DISPLAY "ACCOUNT NUMBER".
ACCEPT WS-ACCNO.
IF WS-ACCNO IS NUMERIC
AND WS-ACCNO > 0
MOVE WS-ACCNO TO D-ACCNO
ELSE
DISPLAY "INVALID ACCOUNT NUMBER"
DISPLAY "PLZ ENTER ACCOUNT NUMBER AGAIN"
GO TO 2000-MAIN
END-IF.
Back to top
View user's profile Send private message
William Thompson

Global Moderator


Joined: 18 Nov 2006
Posts: 3156
Location: Tucson AZ

PostPosted: Tue Aug 14, 2007 6:06 pm
Reply with quote

First, learn what the "Caps Lock" key is used for......
Second, what is wrong with the code?
Back to top
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 691
Location: Earth

PostPosted: Tue Aug 14, 2007 11:41 pm
Reply with quote

Where are you executing this program? I guess not on mainframes.
Maybe you are writing this code in MS COBOL.
Then, just you need to declare the variable ws-accno as numeric, it should accept only numeric digits.
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 632
Location: Wisconsin

PostPosted: Wed Aug 15, 2007 4:32 am
Reply with quote

ACCEPT does not scrub the data for numeric or non-numeric. That has to be taken care of after the fact. Infact it doesn't even know what the data is until you press enter then I am guessing writes it into working storage as hex instead of numeric so non-numerics can be received. It is up to you as the programmer to scrub the data after input.

If you were to accept data from the standard console in any other language on any other platform the data read won't scrub the data either. It just reads and trys to dump the data to the variable. A benefit you may find in other languages is something like this

(What little c++ i remember, it may not be right syntactically)
int var1;
cin >> var1;

Now, the cin statement itself doesn't do any scurbbing of data. However when a non-integer value is input the cin statement will fail because it can't perform the assignment. But the cin (ACCEPT in the case of Cobol) would not fail in and of itself.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Aug 15, 2007 6:03 am
Reply with quote

Hello Satish(?),

Do you have a resolution to your question?

As someone already asked, where are you running this program?

Is there some reason you are using ACCEPT to get data into your code? Most sites do not permit ACCEPT - especially to "retry" a bad entry.

How big is the field you are "accepting" - WS-ACCNO? You need to specify what is an acceptable and what should be an error. Please post some samples of various values and tell us which are "good" and which should be rejected. Keep in mind things like leading or trailing spaces. What about punctuation (dash or comma or . . .)? Once you tell us the rules, we can help on what you might do to validate your field is acceptable.
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 632
Location: Wisconsin

PostPosted: Wed Aug 15, 2007 5:38 pm
Reply with quote

Dick,

Doesn't the code originally posted ensure the field holds numeric data? I read the question as how can I remove checking for numeric and make ACCEPT only take in numeric values. I think the orignal poster doesn't want to do the validataion and thinks ACCEPT should take care of it for him.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Aug 15, 2007 6:24 pm
Reply with quote

Hi Steve,

Quote:
I think the orignal poster doesn't want to do the validataion and thinks ACCEPT should take care of it for him.
Quite possibly. Maybe we will get more definition today.

Quote:
Doesn't the code originally posted ensure the field holds numeric data?
Depends on how this case defines "numeric". For example, some people/situations want 123,456 to be numeric. Time permitting, i'll try to put together a few examples later and run them thru code.
Back to top
View user's profile Send private message
hello2satish

New User


Joined: 21 Jul 2007
Posts: 5
Location: Mumbai

PostPosted: Wed Aug 15, 2007 7:00 pm
Reply with quote

Yes i am writing this code in MS COBOL

my acc-no field is like PIC 9(15)

One thing i must admit that i am not very expert in COBOL like you people

The problem i am facing is like this..

If i enter the acc-no field as 1234abcd then it accept 1234 and truncate abcd and not throwing any error like invalid account number

and if i enter acc-no as abcd then only i am getting the above mentioned error

So my quetion is that it should accept numeric field only and should throw me the error like INVALID ACCOUNT NUMBER if accept anything except numeric value
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Wed Aug 15, 2007 7:10 pm
Reply with quote

You are not likely to find many MS COBOL experts on a mainframe web site.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Aug 15, 2007 7:28 pm
Reply with quote

Hello,

We had to write our own validation routines (this was with MicroFocus COBOL on unix - not MS-COBOL, but also non-mainframe).

Depending on user requirements, the definition of what was a valid numeric changed from case to case. For example, if we had to validate your acc-no, leading spaces would have become zeros and trailing spaces would have been truncated - embedded spaces would have been invalid. If a $ field was being validated, we had to accept numbers, commas, one decimal point, only the proper number of digits after the decimal, a minus-sign at the beginning or end of the value, possibly a currency-sign, and no embedded spaces. We also had to make sure that the "number" would fit in the receiving field.

Quote:
So my quetion is that it should accept numeric field only and should throw me the error like INVALID ACCOUNT NUMBER if accept anything except numeric value
To comletely do this, you will probably need to define your "rules" and implement code to enforce your rules.

You will want to look at the product documentation to see if there are other built-in functions that will give the results you want. If you want complete control, you will need to write routines that both validate the "input" and reformat it to what is acceptable for processing (like filling the left-most bytes with zeros if the numbers entered are not enough to fill the acc-no).
Back to top
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 691
Location: Earth

PostPosted: Thu Aug 16, 2007 9:02 am
Reply with quote

Try this-
Declare the variable as alphanumeric so that it will accept alphanumeric input. Redefine that variable with numeric definition.
Code:
   01 WS-ALPHA-ACC  PIC X(10).
   01 WS-ACCNO REDEFINES WS-ALPHA-ACC  PIC 9(10).
DISPLAY "ACCOUNT NUMBER".
ACCEPT WS-ALPHA-ACC.
IF WS-ACCNO IS NUMERIC
.....

I havent worked in MS COBOL but still, do let us know the results.
Back to top
View user's profile Send private message
Help-Me-Out

New User


Joined: 09 Dec 2006
Posts: 56
Location: Pune

PostPosted: Thu Aug 16, 2007 10:37 am
Reply with quote

Hi,

I don't know MS-COBOL really.
But why don't you try this.

1.Accept the account no in PIC X(15).
2.Check the variable for spaces &/or characters.
3.If not present then valid otherwise invalid.

This is what we do in VS-COBOL II. correct me if wrong.

Thanks.
Sandy
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
No new posts Data set Rec-Cnt and Byte-Cnt Testing & Performance 2
No new posts Calling DFSORT from Cobol, using OUTF... DFSORT/ICETOOL 5
Search our Forums:

Back to Top