Followup To
Question -
Followup To
Question -
int a=0;

The loop will continue until a key is pressed, how do i reconise the key that was pressed?
Answer -
Luis , Thank you for your question.

kbhit is a function, so you must give it an argument list. 'getch' will read the character.

David Spector

Don't understand argument list!

How do you mean getch will read the key pressed?

could you give an example please i want the key pressed to break the loop.

Answer -
Luis, Here is an example. I have not tested this. Let mw know if it does not work or if you do not understand it.

char Key = getch(); // Wait for keystroke


Char Key = getch(); Will wait for a key stroke, but it dose not get the key pressed to return a value to kbhit().

so for example if 'a' was pressed during the loop a '1' for true is returned to kbhit so it drops out of the loop, so if i type 'Char Key = getch() it will wait for me to press another key, but i want to know the key pressed to break the loop. kbhit = 1 because i pressed a key i want to know the key pressed.

Thanks, Luis.

Luis, You are confused about 'kbhit' and 'getch'. Here is how they work:

'kbhit' looks at the keyboard to see if the user has pressed any key. 'getch' reads a keystroke. If the user has pressed a key, 'getch' will return the character value and remove the keystroke from the console buffer.

If you want to wait until the user has pressed a key, then read it, you only have to use 'getch'.

If you want to do some processing until the user has pressed a key, then read it, you have to use 'kbhit' in the processing loop, then 'getch'.



All Answers

Answers by Expert:

Ask Experts


David Spector


Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.


I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2017 About.com. All rights reserved.