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

Content added Content deleted
(Add 8080 assembly)
(Add OCaml)
Line 1,285: Line 1,285:
20 IF INKEY$<>"Y" AND INKEY$<>"N" THEN GOTO 20
20 IF INKEY$<>"Y" AND INKEY$<>"N" THEN GOTO 20
30 PRINT "THE RESPONSE WAS ";INKEY$;"."</lang>
30 PRINT "THE RESPONSE WAS ";INKEY$;"."</lang>

=={{header|OCaml}}==
Unix library is normally bundled with any standard OCaml distribution for POSIX interfaces.
Interfacing with [https://en.wikipedia.org/wiki/POSIX_terminal_interface termios] is specific to UNIX-likes.

<lang OCaml>open Unix
type mode = Prompt | Input
let original_attrs = tcgetattr stdin
let set = function
| Prompt -> tcsetattr stdin TCSAFLUSH {original_attrs with c_icanon = false}
| Input -> tcsetattr stdin TCSAFLUSH original_attrs
let getchar buf =
if read stdin buf 0 1 = 0 then raise End_of_file
else Bytes.get buf 0</lang>

Main:
<lang OCaml>let _main =
let buf = Bytes.create 1 in
let rec prompt () =
print_string "Prompt? [Y/N]: ";
flush_all();
set Prompt;
match getchar buf with
| 'n'|'N' -> ();
| 'y'|'Y' -> print_endline ": Ok."; prompt ();
| _ -> print_endline ": Invalid."; prompt ();
set Input;
in
prompt ()</lang>


=={{header|Oforth}}==
=={{header|Oforth}}==