Ok, so I'm working on the final project for my Computer Science 2 class and I'm writing a Who Wants To Be A Millionaire game for it. I'm using GetAsyncKeyState in a time controlled while loop for the questions so that I could implement a timer. Problem is, after I press a key and call another menu, it automatically puts the previous key(s) in. Is there any way to clear the keyboard buffer for GetAsyncKeyState?
I found somewhere that said to use this:
1 2 3 4
if (cin.peek() != EOF)
{
cin.ignore(numeric_limits<streamsize>::max());
}
The program won't run with that so I found that I could replace numeric_limits<streamsize>::max() with LONG_MAX.
However that causes the program to pause indefinitely. I need something that won't cause the program to pause at all.
Here's the loop I'm currently using for the timer:
GetAsyncKeyState doesn't work with a buffer. That's what the "Async" (asynchronous) part of its name means. It just checks if the key is pressed when you call it. The problem arises simply because you don't give the user enough time to take his finger from the key... Try sleeping for more time, Sleep(1000); for example, this would do the job, I suppose.
Doesn't work. When a key is pressed that tells it to go to another menu, it then enters that previous key for the next menu as well. Or, since here it will only execute if you enter a letter, if you press a bunch of numbers and/or symbols nothing happens, but then if you press a letter it outputs all the numbers and/or symbols that you pressed before to the screen as input. It's somehow logging all keystrokes and not clearing them out.
Nevermind, I took another look through my code, line by line, and found that in my checkAns function when it goes to check if the entered answer was correct I still had several spots that were using cin, and that appears to solve the issue. Apparently I can't use cin anywhere after I use GetAsyncKeyState, so I'm in the process of converting all input portions of the program to use GetAsyncKeyState. Also, where I was having the problem of the input not working, it was because I wasn't treating the number option entered as an ascii value, rather I was treating it as a plain old int.
Thanks anyway though! I'll be back if I have anymore issues :P