I am making typing game in which random alphabets falls down from top of screen to the bottom and user needs to press that key to get score. There are two nested loops use to make this falling effect. The outer while loop generates random alphabet and the random position on x-axis while the inner for loop increments y-axis coordinates and prints the characters for each y coordinate value to make it fall. Now the problem is that when I use kbhit() function in for loop to check if users have pressed any key or not, it returns false when user has not pressed any key. But when user presses a key for the first time, it returns true and user gets the score. But when kbhit() is called again for next random alphabet, it returns true whether or not user has hit keyboard or not because user pressed ket earlier. May be I need to clear the keyboard buffer but I don't know how to do that. Here is
while (true) {
ch = rand() % 26 + 65;
xPos = rand() % (x_end - x_start - 1) + x_start + 1;
for (int i = y_start + 1; i < y_end - 1 && !kbhit(); i++) {
cur_pos.X = xPos;
cur_pos.Y = i;
SetConsoleCursorPosition(console_handle, cur_pos);
Sleep(150);
cout << " ";
cur_pos.X = xPos;
cur_pos.Y = i + 1;
SetConsoleCursorPosition(console_handle, cur_pos);
cout << ch;
if (i == y_end - 2) {
cur_pos.X = xPos;
cur_pos.Y = i + 1;
SetConsoleCursorPosition(console_handle, cur_pos);
cout << ch;
Sleep(150);
cur_pos.X = xPos;
cur_pos.Y = i + 1;
SetConsoleCursorPosition(console_handle, cur_pos);
cout << " ";
}
}