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

→‎{{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()
{
charint chc;
while(1) {
set_mode(1);
do{
while (get_key(0)); /* clear buffer */
printf("\nDo you want to see this prompt again (Y/N) :");
printf("\nWhatPrompt theagain heck[Y/N]? !");
fflush(stdin);
scanffflush("%c",&chstdout);
 
}while(ch=='y'||ch=='Y');
c = get_key(1);
if (chc == 'nY' ||ch c == 'Ny') {
printf("\n");
{
continue;
printf("\nYou opted not, so toodleedoo......");
}
 
}whileif (chc == 'yN' ||ch c == 'Yn'); {
printf("\nWhat the heck !");
printf("\nDone\n");
return 0 break;
}
 
</lang>
printf("\nYes or no?\n");
}
 
return 0;
}</lang C>
 
=={{header|Common Lisp}}==
 
Anonymous user