I want to take input in a loop without stopping its execution i.e.
`
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
char c;
while(1)
{
if((c=getch())=='y')
printf("yesn") ;
printf("non") ;
}
return 0;
}
Now i want that “no” should be printed infinitely regardless of input and if i press y then yes should be printed.And then continuing from no again. Is this possible, any IDEA!
8
Since this appears to be on Windows and you’re already using the old conio functions, you could double down with _kbhit()
:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
while(1)
{
if(_kbhit() && getch() == 'y')
printf("yesn");
printf("non") ;
}
return 0;
}
_kbhit()
“checks the console for a recent keystroke,” according to the documentation. What that means is that if _kbhit()
is true, getch()
will be able to get a character immediately and not block.