kbhit function in c++

Explain me how to use kbhit function in turbo c++.

1 Like
3 Likes

Also i recommend you to switch to other compiler like DEV C++ or VS…as Turbo c interface is very poor as compared to dev C
…
So Download dev c from this link…trust me…you will enjoy writing the code…

http://sourceforge.net/projects/orwelldevcpp/files/latest/download

3 Likes

This function is not defined as part of the ANSI C/C++ standard. It is generally used by Borland’s family of compilers. It checks if any keystroke is there in the keyboard buffer. If yes, then it returns a non zero integer and the keystroke can be retreived using getch() or getche() else return 0. A short segment of code may help more:

int main()
{
cout<<"Press any key : ";
while(!kbhit())
;
cout<<"You pressed a key :) You may exit now ....  \n";
cout<<"THANKYOU :P";
return 0;
}

kbhit function is used to determine if a key has been pressed or not. To use kbhit function in your program you should include the header file “conio.h”. If a key has been pressed then it returns a non zero value otherwise returns zero.

#include <stdio.h>
#include <conio.h>
main()
{
while (!kbhit())
printf("You haven't pressed a key.\n");
return 0;
}

As long as in the above program user doesn’t presses a key kbhit() return zero and (!0) i.e. 1 the condition in while loop is true and “You haven’t pressed a key.” will be printed again and again. As a key is pressed from the keyboard the condition in while loop become false as now kbhit() will return a non-zero value and ( !(non-zero) = 0), so the control will come out of the while loop.

www.quora.com/profile/Tridib-Samanta-2