How do I take in a keystroke, but only if there is one?

Hey guys. I know this is probably a really simple problem, but I googled it and found things, but nothing that answered this.

My main chunk of my program is in a big loop. I want to make it so if I hit a key, say, 'q', it will break from the loop and go elsewhere (basically, this is because as it is right now, it prints the data to file after the loop is done, so if I decide I want to cancel the run, I lose all the data I have so far. I'd like to make it so I can quit and let the program close itself, after writing the data to a file).

How would I do this, getchar()?

Thanks!
You can ask std::cin's streambuf how many characters are in its input buffer.

http://www.cplusplus.com/reference/iostream/streambuf/in_avail/


Hmmmm. I don't think this will work, because I have to hit enter for the input to stop, because of this line:

cin >> ch;

Is there any way to take input without having to hit enter for it to continue?

edit: For example, like the way top works. I hit q, don't have to hit enter, and it just exits.
Last edited on
A bit old, but I think this post asks the same thing:
http://www.cplusplus.com/forum/beginner/3329/
Hmmmm. I'm on Linux. I searched more and found this: http://cboard.cprogramming.com/faq-board/27714-faq-there-getch-conio-equivalent-linux-unix.html

but it doesn't do what I need it to. That (above) waits for input from the user, though immediately acts on the input, without waiting for enter. However, it still WAITS for input. I need something that doesn't wait for it, just acts if it gets it.
Here's a post that is what you want.

http://www.cplusplus.com/forum/unices/11910/
Topic archived. No new replies allowed.