Reputation: 127
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.
Upvotes: 2
Views: 7626
Reputation: 11
#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...
Upvotes: 1
Reputation: 417
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.
Upvotes: 2
Reputation: 11
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.
Upvotes: 1
Reputation: 5379
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).
Upvotes: 0
Reputation: 167
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 :)
Upvotes: 0