I'm just learning how to use GetCh(). I have the GetCh() call inside a do...while loop and I think I'm having a problem where sometimes the program is not waiting for the user to press a key, instead it is using the same key that was pressed the last time GetCh() was called and continuing on until GetCh() is called again in the next time through the loop.
My actual code is quite long, so I don't want to bore you guys with it, but this is an approximation of what I have:
However, when I use this code, I notice when I'm debugging it that the character located at *KeyPress is the inputted character followed by a series of garbage characters. Though, the code still functions, I believe this is probably sloppy coding. Can anybody help me clean this up or tell me what I'm doing wrong?
I don't actually understand any of the code in the CHAR GetCh (VOID) function. I just copied and pasted it from a forum post from somebody suggesting a good way to get keyboard input without echo. Is there a way to adjust this so it is looking for a char instead of a string?
On second look, your code is fine. I ran it through my debugger, and it works with no memory leaks. My comment above is wrong.
I think what you're asking is "is my code sloppy" and the answer is dependent on who is looking at it.
If this is part of a large program that will be maintained for a long while, and your platform supports c++11 style coding, you might consider using auto pointers instead of new/delete.
If this is part of a class assignment where getting input from the keyboard is simply a way to get data into the program, I'd say it works... ship it.
There is a very long list of possibilities as to how to accomplish any task. That's part of why I like C++.
I'm working on the Dungeon Crawl question from http://www.cplusplus.com/forum/articles/12974/
Just for my own amusement and to work on my C++ skills (I'm still a beginner and learning)
It is starting to get large (over 350 lines in int main() )
I'm using the wads keys to move the player around the board. I haven't proven it yet, but I'd swear that sometimes when I press a direction he jumps ahead two spaces instead of one. Is there maybe a way to flush the input buffer before checking for input the next time the loop comes around?
...and your platform supports c++11 style coding
I'm not really sure what that means or how I check. Do you mean what OS I have (i.e. Windows 7) or what compiler I'm using (i.e. Visual Studio 2010)?
I've been doing a bit more testing and now I am definitely getting occurances of the program skipping where it's supposed to wait for keyboard input from the user and instead running through the loop again using the KeyPress value from the previous loop. Any ideas?