Ok, so I'm making a program and int main() is basically a start menu. I'm using _getch so when the user presses a key, it will send them to whatever option they pressed. ex: they want to show results, int main() redirects them to the function that displays the results. The menu is in a "while()" loop and is infinite until the user quits the program. For some reson though, _getch() is registering characters even while the program is executing another function. After the function returns, _getch() will cause the menu to go to an option based on the first button you pressed while in the 'show results' function (or whatever function was being executed). so, if you decide you want to go to the options menu from the start menu, and you press '1' in the optiuons menu, then return to the start menu, whatever is '1' in the start menu will be executed. I think it has somthing to do with the while() loop. Can anyone help me?
Hard for us to answer, since _getch() isn't part of "real" C++, is system dependent, and doesn't even exist in many/most compilers.
And, as far as I know, there is no way to implement this functionality in a system-independent manner in C++. So, if it's working for you, great, but...be prepared to have to modify your code if you ever move your program to another platform.
nice, dont care, cause _getch works with windows just fine. Even if it doesnt, you didnt help me at all.
i found the answer anyway.
while(_kbhit()) _getch();
apparently, _getch() is a stream function, so if you execute another function, _getch() will immediately execute any buttons pressed even though the function getch is in isnt being executed at the moment. The statement above clears the stream.