C++/c++ graphics
Expert: Zlatko - 8/7/2010
Questionhi sir
act i have made simple program in c++ to move a circle left of right by pressing 1 for left and 2 for right and i have to press enter after pressing 1 or 2
but how i will get d desire move without pressing enter
AnswerHello Kitty.
There is a function called kbhit which will return non-zero if there is a character in the keyboard buffer. You can use it to see if the user pressed a key. Then you can use getch to read the character to see if it is a 1 or 2.
If there is no character in the keyboard buffer, kbhit will return 0.
The code would look something like this:
#include <conio.h>
void example(void)
{
while(true)
{
if (_kbhit())
{
char ch = (char)_getch();
if (ch == '1')
{
// Move circle right
}
else if (ch == '2')
{
// Move circle left
}
}
}
}
On my system, the kbhit and getch functions start with an underscore character.
I hope that helps you.
Best regards
Zlatko