User 154806
User 154806

Reputation: 95

How to prevent console output after exiting the program

I'm writing simple command-line program in C++ (windows). One functionality of it is to check keyboard input and if a certain key is pressed, exit the program.

Actually it works fine so far, however when the program exits, I get all the pressed key on the output of the command-line? Is there a way to avoid this behavior?

Thanks in advance...

Upvotes: 2

Views: 369

Answers (4)

engf-010
engf-010

Reputation: 3929

Use _kbhit() to test if a key is pressed. and when it is use _getch() to get the value. (note _getch() can return 0 or 0xE0 for special keys and call _getch() again for that value)

Upvotes: 0

finiteautomata
finiteautomata

Reputation: 3813

You can use getch() which is supposed not to echo the character. I can't remember the header in which it's defined, but I think it was conio.h (remember that it's not a standard header)

Upvotes: 0

Didier Trosset
Didier Trosset

Reputation: 37467

It depends on how you check for keyboard input.

You probably check for the input without actually using it, thus leaving it in the input queue for the next program able to get it (the command line).

Upvotes: 0

escargot agile
escargot agile

Reputation: 22389

getch has no echo, meaning it does not print the character you type. Here is some more info about all the get char functions in C/C++:

http://www.daniweb.com/forums/thread37195.html

Upvotes: 3

Related Questions