Keyboard input/Obtain a Y or N response: Difference between revisions

Content added Content deleted
m (added whitespace before the TOC (table of contents), hyphenated key-press.)
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.)
Line 984: Line 984:
<br>Some older Classic REXX interpreters have a keyboard read subroutine (BIF) so that the program can read keyboard keys as
<br>Some older Classic REXX interpreters have a keyboard read subroutine (BIF) so that the program can read keyboard keys as
<br>they are pressed &nbsp; (see the other versions below).
<br>they are pressed &nbsp; (see the other versions below).
<lang rexx>/*REXX program tests for a Y or N key when entered after a prompt.*/
<lang rexx>/*REXX program tests for a Y or N key when entered from keyboard after a prompt.*/


do queued(); pull; end /*flush stack if anything queued. */
do queued(); pull; end /*flush the stack if anything is queued*/


prompt = 'Please enter Y or N for verification:' /*PROMPT msg.*/
prompt = 'Please enter Y or N for verification:' /*this is the PROMPT message.*/


do until pos(ans,'NY')\==0 & length(ans)==1 /*··· Y | N ?*/
do until pos(ans,'NY')\==0 & length(ans)==1 /*keep looking for a Y or N answer.*/
say; say prompt /*show blank line, show the prompt*/
say; say prompt /*display blank line; display prompt. */
pull ans /*get the answer(s) & uppercase it*/
pull ans /*get the answer(s) and uppercase it.*/
ans = space(ans,0) /*elide all blanks. */
ans=space(ans, 0) /*elide all blanks. */
end /*until*/
end /*until*/
/*stick a fork in it, we're done.*/</lang>
/*stick a fork in it, we're all done. */</lang>


===version 1 for PC/REXX and Personal REXX===
===version 1 for PC/REXX and Personal REXX===
This version of a REXX program works with PC/REXX and Personal REXX.
This version of a REXX program works with PC/REXX and Personal REXX.
<lang rexx>/*REXX program to test for a Y or N key when pressed. */
<lang rexx>/*REXX program tests for a Y or N key when entered from keyboard after a prompt.*/
prompt = 'Please press Y or N for verification:' /*PROMPT msg.*/
prompt = 'Please enter Y or N for verification:' /*this is the PROMPT message.*/


do until pos(ans,'NYny')\==0 /*keep prompting until answer = Y N y n */
do until pos(ans, 'NYny') \== 0 /*keep prompting until answer= Y N y n */
say; say prompt /*show blank line, then show the prompt. */
say; say prompt /*display blank line; display prompt. */
ans=inkey('wait') /*get the answer(s) from the terminal. */
ans=inKey('wait') /*get the answer(s) from the terminal. */
end /*until*/
end /*until*/
/*stick a fork in it, we're all done. */</lang>
/*stick a fork in it, we're all done. */</lang>


===version 2 for PC/REXX and Personal REXX===
===version 2 for PC/REXX and Personal REXX===
This version is the same as above, but has a more idiomatic technique for testing the response.
This version is the same as above, but has a more idiomatic technique for testing the response.
<lang rexx>/*REXX program to test for a Y or N key when pressed. */
<lang rexx>/*REXX program tests for a Y or N key when entered from keyboard after a prompt.*/
prompt = 'Please press Y or N for verification:' /*PROMPT msg.*/
prompt = 'Please enter Y or N for verification:' /*this is the PROMPT message.*/


do until pos(ans,'NY')\==0 /*keep prompting until user answers Y | N*/
do until pos(ans, 'NY')\==0 /*keep prompting 'til user answers Y│N */
say; say prompt /*show blank line, then show the prompt. */
say; say prompt /*display blank line; display prompt. */
ans=inkey('wait'); upper ans /*get the answer(s), also, uppercase it. */
ans=inKey('wait'); upper ans /*get the answer(s); and uppercase it.*/
end /*until*/
end /*until*/
/*stick a fork in it, we're all done. */</lang>
/*stick a fork in it, we're all done. */</lang>


=={{header|Ring}}==
=={{header|Ring}}==