Keyboard input/Obtain a Y or N response: Difference between revisions
m (→{{header|Ada}}: remove flushing output) |
(→Common Lisp: new example) |
||
Line 30: | Line 30: | ||
60 PRINT "The response was "; k$ |
60 PRINT "The response was "; k$ |
||
=={{header|Common Lisp}}== |
|||
⚫ | |||
<lang lisp>(defun rosetta-y-or-n () |
|||
(clear-input *query-io*) |
|||
(y-or-n-p))</lang> |
|||
=={{header|Inform 7}}== |
=={{header|Inform 7}}== |
||
Line 121: | Line 125: | ||
{{omit from|PARI/GP}} |
{{omit from|PARI/GP}} |
||
⚫ |
Revision as of 04:11, 23 December 2010
You are encouraged to solve this task according to the task description, using any language you may know.
Obtain a valid Y or N response from the keyboard. The keyboard should be flushed, so that any outstanding keypresses are removed, preventing any existing Y or N keypress from being evaluated. The response should be obtained as soon as Y or N are pressed, and there should be no need to press an enter key.
Ada
<lang Ada> function Yes_Or_No (Prompt : String := "Your answer (Y/N): ") return Boolean is
Answer : Character; begin Ada.Text_IO.Put (Prompt); loop Ada.Text_IO.Get_Immediate (Answer); case Answer is when 'Y'|'y' => return True; when 'N'|'n' => return False; when others => null; end case; end loop; end Yes_Or_No;</lang>
BASIC
ZX Spectrum Basic
10 IF INKEY$<>"" THEN GO TO 10 20 PRINT "Press Y or N to continue" 30 LET k$ = INKEY$ 40 IF k$ = "y" OR k$ = "Y" OR k$ = "n" OR k$ = "N" THEN GO TO 60 50 GO TO 30 60 PRINT "The response was "; k$
Common Lisp
<lang lisp>(defun rosetta-y-or-n ()
(clear-input *query-io*) (y-or-n-p))</lang>
Inform 7
Keyboard input goes through a virtual machine that's only required to provide blocking input operations, so flushing the buffer isn't possible.
Inform 7 has a built-in function to ask the user for yes-or-no input, but it requires them to press enter afterward: <lang inform7>Qwantz is a room.
When play begins: say "A wizard has turned you into a whale. Is this awesome (Y/N)? "; if the player consents, say "Awesome!"; end the story.</lang>
To read a single key without waiting for enter, we can redefine the function by including a snippet of Inform 6 code: <lang inform7>To decide whether player consents: (- (YesOrNoKey()) -).
Include (- [ YesOrNoKey ch;
do { ch = VM_KeyChar(); } until (ch == 'y' or 'Y' or 'n' or 'N'); return ch == 'y' or 'Y';
]; -).</lang>
PicoLisp
<lang PicoLisp>(de yesno ()
(loop (NIL (uppc (key))) (T (= "Y" @) T) (T (= "N" @)) ) )</lang>
PureBasic
Inkey() returns the character string of the key which is being pressed at the time. <lang PureBasic>PrintN("Press Y or N to continue")
Repeat
; Get the key being pressed, or a empty string. Key$=UCase(Inkey()) ; ; To Reduce the problems with an active loop ; a Delay(1) will release the CPU for the rest ; of this quanta if no key where pressed. Delay(1)
Until Key$="Y" Or Key$="N" PrintN("The response was "+Key$)</lang>
REXX
REXX (in general) requires the user to press ENTER when
entering text.
Some REXX interpretors have a keyboard read subroutine so that the
program can read keyboard keys as they are pressed.
<lang rexx>
do queued() /*flush the stack if any lines queued. */ pull end
prompt='Press Y or N for some reason.' /*prompt message*/ ok='Y N' /*acceptable answers (will be uppercase)*/
do forever say /*write a blank line for visual fidelity.*/ say prompt /*prompt the user for an input. */ pull ans /*get the answer(s), also, uppercase it. */ ans=strip(ans) /*get rid of leading/trailing blanks. */ if ans= then iterate /*if blank, try again.*/ if wordpos(ans,ok)\==0 then leave /*if ans is OK, leave.*/ end
/*as this point, ANS holds a Y or N. */
</lang>
Tcl
<lang tcl>proc yesno Template:Message "Press Y or N to continue" {
fconfigure stdin -blocking 0 exec stty raw read stdin ; # flush puts -nonewline "${message}: " flush stdout while {![eof stdin]} { set c [string tolower [read stdin 1]] if {$c eq "y" || $c eq "n"} break } puts [string toupper $c] exec stty -raw fconfigure stdin -blocking 1 return [expr {$c eq "y"}]
}
set yn [yesno "Do you like programming (Y/N)"]</lang>