Note that there are some explanatory texts on larger screens.

plurals
  1. POkbhit() as escape trigger
    text
    copied!<p>I want to use kbhit() for "Press any key to continue" function. However, after I used the kbhit() in a loop, the key-pressed is stored in the stdin. So in the next scanf(), the key-pressed from before, appears in the input.</p> <pre><code>int x,b=0; printf("Press any key to continue...") while (b==0) { b=kbhit(); } system("cls"); printf("Enter number:"); scanf("%d",&amp;x); </code></pre> <p>So, if the user pressed a key, lets say the letter K, the k appears after "Enter number:".</p> <p>I've tried looking for solutions, but failed to make any of them work. I tried to put a backspace character in to the input stream. I also tried using getch(), however, the user has to press "Enter" in order to continue, so it defeats the original purpose. I also tried clearing the stdin stream, by closing and opening, but I can't get it to open properly.</p> <p>EDIT: As what janisz said in the comments, all I needed is to use system("pause"). Although I can't edit As what janisz said in the comments, all I needed is to use system("pause"). Although I can't edit the "Press any key to continue", its sufficient for my purpose. I will continue trying other solutions provided here for better results if possible, but for now, system("pause") is want i need.</p> <p>EDIT2: Ok, some of you suggested using getch(). From what I saw online, getch() function gets the input from the stream without the char actually showing on the screen, which is what I want. However, when I tried using getch(), the program doesn't continue after I press any key, it waits for me to press the enter key. Is there a problem? I'm using C-Free 4 Standard on Windows 7.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload