Anonymous user
Keyboard input/Obtain a Y or N response: Difference between revisions
Keyboard input/Obtain a Y or N response (view source)
Revision as of 17:46, 28 June 2011
, 12 years ago→{{header|C}}: replaced non-portable code -- fflush(stdin) and conio.h
(added ruby code) |
(→{{header|C}}: replaced non-portable code -- fflush(stdin) and conio.h) |
||
Line 45:
=={{header|C}}==
For POSIX compliant systems (in theory that includes WinNT family).
<lang C>▼
<lang C>#include <stdio.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
void set_mode(int want_key)
{
static struct termios old, new;
if (!want_key) {
tcsetattr(STDIN_FILENO, TCSANOW, &old);
return;
}
tcgetattr(STDIN_FILENO, &old);
new = old;
new.c_lflag &= ~(ICANON);
tcsetattr(STDIN_FILENO, TCSANOW, &new);
}
int get_key(int no_timeout)
{
int c = 0;
struct timeval tv;
fd_set fs;
tv.tv_usec = tv.tv_sec = 0;
FD_ZERO(&fs);
FD_SET(STDIN_FILENO, &fs);
select(STDIN_FILENO + 1, &fs, 0, 0, no_timeout ? 0 : &tv);
if (FD_ISSET(STDIN_FILENO, &fs)) {
c = getchar();
set_mode(0);
}
return c;
}
int main()
{
while(1) {
set_mode(1);
while (get_key(0)); /* clear buffer */
}while(ch=='y'||ch=='Y');▼
c = get_key(1);
if (
printf("\n");
continue;
}
▲ printf("\nWhat the heck !");
printf("\nDone\n");
}
printf("\nYes or no?\n");
}
return 0;
=={{header|Common Lisp}}==
|