0

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("yes\n") ;

        printf("no\n") ;
     }
      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!

1 Answers1

0

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("yes\n");

    printf("no\n") ;
  }
  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.

Wintermute
  • 42,983
  • 5
  • 77
  • 80