2

I'm working on a project and I need to implement a loop that runs and execute a function (foo) until a user enters any input

I already tried some implementations none of them worked (I'm aware that getchar isn't ideal for this operation) for example:

while(1)
{
    foo();       //Just a dummy function
    if(getchar())//If the user didn't enter an input keep on running
    {
        break;
    }
}

I expect the loop to keep on running and break when the user enters any kind of input.

  • @Someprogrammerdude but we can always write the one we need. Even if it requires writing the kernel driver – 0___________ Jul 07 '19 at 19:27
  • MSVC has the extension `kbhit()` although it is specific to the keyboard, not "any kind of input". I have always been disappointed by the weak and IMO almost useless standard function `getchar()`. You might as well use `fgets()` as that. There have been a stupendous number of problems arisen from the horrible way that `getchar()` was implemented. – Weather Vane Jul 07 '19 at 19:42

1 Answers1

2

If you're on Windows, you'll probably have kbhit() function. If you're on Linux, read this: Using kbhit() and getch() on Linux