COBOL 85 yes or no input validation

577 views Asked by At

i just want to know how to make sure that the input entered by the user is only Y, y, N, or n.

I used this code:

01 ANSWER                PIC X.
    88 VALID-ANSWER      VALUE "Y" "y" "N" "n".

But still it doesn't work. The user can still type other characters. Any help will be appreciated. Thanks in advance.

2

There are 2 answers

1
Molusco On

You just can't restrict the user input, except for uppercase. What you can do is this:

At Working-Storage:

01 ANSWER           PIC X.

At Procedure:

ACCEPT ANSWER CONTROL "UPPER".

That will make sure whatever the user inputs, it's full uppercase. Then you check only for a "Y":

IF ANSWER = "Y"
  (do something)
ELSE
  (do something)
END-IF

I hope it helps.

1
U. Fleu On

I solve this by using a perform loop:

at WORKING-STORAGE:
    01 ANSWER PIC X.

at PROCEDURE:
    PERFORM UNTIL ANSWER = "y" OR = "Y" OR = "n" OR = "N"
       DISPLAY "Question (y/Y/n/N) :"
       MOVE SPACE TO ANSWER
       ACCEPT ANSWER
    END-PERFORM.
    IF ANSWER = "y" OR "Y"
       do-something
    ELSE
       do-something-else
    END-IF

I hope this helps.