Search code examples
cloopsgotostdioconio

kbhit() with double loop not working well


Just for fun, I tried printing kbhit() with loops, so that the program after a key press prints the line infinitely until pressed keyboard again. It compiles well and when run, just gives blank screen. No prints. But upon single keypress ends the program. The console does not close though.

#include <stdio.h>
#include <conio.h>

int main()
{
  while(1)
  {
    if(kbhit())
    {
      while(1)
      {
        if(kbhit())
        {
          goto out;
        }
        printf("Print Ed Infinitum Until Key Press");
      }
    }
  }
  out:
  return 0;
}

How do I solve this?


Solution

  • int main(void){
        while(1){
            if(kbhit()){
                getch();
                while(1){
                    if(kbhit()){
                        getch();
                        goto out;
                    }
                    printf("Print Ed Infinitum Until Key Press\n");
                }
            }
        }
    out:
        return 0;
    }