I working with an application written in Visual Studio C++. Then program is a console application running under the DOS-prompt. The program is something like this.

void main ()
{
    for ( ;; )
    {
        do_something ();
        do_something_more ()
    }
}

Now I need to add some code between do_something () and do_something_more () that checks if any key on the PC-s keyboard is pressed, and if so read that key. The problem is that the program must continue with do_something_more () if no key is used. I can not wait for the keypress, the program must continue executing as before if no key is pressed. I have not been able to figure out how to do this for this type of application. Anybody can give me a hint of how to do this?

Recommended Answers

All 5 Replies

Visual C++ supports kbhit() and getch() in the conio.h library, together they do what you want.

GetAsyncKeyState() can also be used for this purpose. Check this.

And main() is NOT void. It's int.

commented: Read well in the future the post and ake sure that you see the similarities before flagging bad post. i dont want to do this but you refused to read carefuly the(my) post before you flagged. -1

Thanks a lot, this was exactly what I needed.

And always main() must be int method not void as it returns a value.

commented: You must work with the Department of Redundency Department. Am I not clear? Do you people have to repeat everything that I say? -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.