tau
tau

Reputation: 1

Way to "catch" input data through ACCEPT?

I am fairly new to Cobol and am writing a basic application to get started with the language. Right now it is a 100% console application and I ran into a problem that isn't really a problem unless the user inputs the wrong data... And after not being able to find an answer to my "problem" through google I can't seem to get it out of my head, hence the thread here.

I have a variable USER-RECORD X(4) and during run-time I ask the user to enter his/her user record. Let's say they enter '1234', then 1234 is being saved and stored in a file along with the other data being requested. However, if the user were to enter "11234" by mistake, then the program will store 1123 instead of 1234, which is wrong because of the wrongly entered data.

My question is if it is possible to surround the ACCEPT verb with some kind of statement, or "catch" the incoming data in some way that if the data being sent is larger than 4 characters something will happen? It is probably smarted to handle these things afterwards making the user confirm the data and all, but I can't seem to get this out of my head. Is this possible at all?

   01  USER-RECORD                                     PIC X(4).
   01  USER-RECORD-REDEFINED REDEFINES USER-RECORD     PIC 9(4).

   GET-USER-INPUT.
       MOVE 'N' TO WS-NUMERIC.
       PERFORM UNTIL WS-NUMERIC = 'Y'
           DISPLAY 'ENTER YOUR 4 DIGIT RECORD NUMBER: ' NO ADVANCING
           ACCEPT USER-RECORD *>---THIS IS WHERE MY QUESTION LIES---
           IF USER-RECORD-REDEFINED IS NUMERIC
               MOVE 'Y' TO WS-NUMERIC
           ELSE
               DISPLAY 'VALUE WAS NOT NUMERIC OR 2 SHORT, TRY AGAIN'
       END-PERFORM.

Upvotes: 0

Views: 1640

Answers (1)

Bill Woodger
Bill Woodger

Reputation: 13076

Your Micro Focus compiler supports the SCREEN SECTION. If you use a SCREEN, then your data can only be entered up to the size of the field.

See the FORMAT 4 of ACCEPT in the Micro Focus documentation here, https://supportline.microfocus.com/documentation/books/nx51ws01/nx51indx.htm, and from there locate the documentation of the SCREEN SECTION and other documentation about using screens in Micro Focus COBOL.

ACCEPT and DISPLAY are the COBOL verbs which suffer the most difference from compiler to compiler. When COBOL was originally developed, the idea of a user sitting at a screen to interact with a program was, let's say, futuristic.

Assuming that you have already seen the effect you describe and want to use a very simple, and standard-ish, format of ACCEPT, I'd suggest this:

01  USER-RECORD                                     PIC X(80).
01  FILLER REDEFINES USER-RECORD.
    05  USER-ID-give-it-a-good-name                 PIC 9(4).
    05  FILLER                                      PIC X(76).
        88  USER-RECORD-EXTRAS-BLANK                VALUE SPACE.

SET USER-RECORD-EXTRAS-BLANK TO TRUE
ACCEPT USER-RECORD
[your existing code using the nice new name]
IF NOT ( USER-RECORD-EXTRAS-BLANK )
    [do something for also an error]
END-IF

The user may be able to circumvent this by idly using the spacebar to get 76 blanks, but probably not worth dealing with that.

Upvotes: 1

Related Questions