2

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.

int x,b=0;    
printf("Press any key to continue...")
while (b==0) {

    b=kbhit();

    }
system("cls");

printf("Enter number:");

scanf("%d",&x);

So, if the user pressed a key, lets say the letter K, the k appears after "Enter number:".

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.

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.

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.

Raam Kumar
  • 127
  • 2
  • 11
  • 1
    Look into adding a call to the `fpurge(FILE *stream)` function right after the while loop. _purge_ erases any input or output buffered in the given stream. It can be found in . – Nocturno Dec 02 '12 at 14:50
  • fflush only flushes output stream. It didn't work for me. fpurge is unavailable, its not in my stdio.h. I think its only available on Linux. – Raam Kumar Dec 02 '12 at 15:02
  • 1
    You can use `system("pause");` instead `kbhit()` – janisz Dec 02 '12 at 15:18
  • 1
    It is time to start sending those who teach students to use `system("cls");` to jail for life. –  Dec 02 '12 at 15:20
  • Thank you janisz. I never thought there was a solution so simple. Exactly what I needed. – Raam Kumar Dec 02 '12 at 15:25

7 Answers7

2

kbhit() returns an integer value indicating whether the user has pressed a key or not. Please note that the key pressed still remains in the buffer. All you have to do is to flush the stdin buffer by using fflush(stdin) statement.

However if you want to use the key pressed by the user you will have to use a getch() or scanf statement after you have used kbhit().

You may read a good article on "How to use kbhit in C and C++" here for exact usage instructions.

Preeti Kaur
  • 419
  • 4
  • 5
1

see http://support.microsoft.com/kb/43993 essentially, insert this code after you read the character you want:

while (kbhit()) getch();         //clear buffer
fflush (stdin) ;         // clear stdin's buffer

you need to flush both the bios kb buffer and stdin.

C. A. McCann
  • 76,893
  • 19
  • 209
  • 302
mistermeta
  • 11
  • 2
1
#include <windows.h>`#include <windows.h>
#include <stdio.h>
#include <conio.h>      // for kbhit() only
#include <stdbool.h>    // for booleans

void cleaningBuffers()
{
    fflush(stdout);                         // always ok & need!
    while (kbhit()) getch();                // clear buffer
    if ( fflush(stdin) == 0 ) {;}           // undefined, not enough
    // flush the bios kb buffer:
    if ( fflush(__iob_func()) ==  0) {;}    // now ok
}

and console buffers is clear...

0

you should consider flushing the input stream after the key was pressed

int x,b=0;    
printf("Press any key to continue...")
for(;;){
    if(kbhit()){
       fflush(stdin);
       break;
    }
}
system("cls");

printf("Enter number:");

scanf("%d",&x);

Now your x variable is clean and pretty :)

Ostap Koziy
  • 167
  • 1
  • 2
  • 9
  • It made no difference. the key-pressed still appears by the default at the scanf – Raam Kumar Dec 02 '12 at 15:21
  • you can try then to read the char that toggles the kbhit() and ignore it. it's a solution because there will always be one char to ignore, but that is 'very bad style' – Ostap Koziy Dec 07 '12 at 20:24
0

kbhit() is in conio.h, it's a console function. It will not be affected by rediction (but fflush will!). Thus to "eat off" the key pressed, you should use getch(), which is also a console function. As an added bonus, it will only eat off one character, not all.

Edit: Only on rereading your question i wonder: why not use getch() just like that? The kbhit() is useless, unless you do something in the loop.

Furthermore, the POSIX compliant function names would be _getch() and _kbhit() (at least on planet Microsoft).

cxxl
  • 4,939
  • 3
  • 31
  • 52
0

Try this

while(!kbhit());
getch();
kishu27
  • 3,140
  • 2
  • 26
  • 41
-1

u can use getchar()...it'll scan and also display onscreen