Ignoring getch()

I was wondering if there was a way to ignore getch() after an amount of time has passed.
Like, if i wanted the user to press a key within 5 seconds of me telling them to, or else the program closes, or something like that. How would I do that, or is there something other than getch() that has to bee used?
You shouldn't be using the console at this point, it's best to move on to Windowed programming. Check out SFML, for example.
The best way to do this would be to use threads.
One for the 5 second timer, one for waiting for input.
That's actually the worst possible way to do this. Never use threads with console input.
It seems you two have different opinions :P. I checked out SFML, and ill probably get into that. I'm just using console until i learn some more in a class im taking soon.
SDL is pretty fun solution too.
@L B Why is that?

Make sure there are no other input operations while the thread is running. Otherwise, behavior is undefined. ;)
Last edited on
@Superdude: Because threads and console I/O are generally messy and hard to get right (just like threading in general) except moreso since console I/O is not meant to be used with threads.
One f the more easier alternatives for beginners would be to use _kbhit() from the (severely depricated) conio.h header file. it will return true if a key was pressed (or if there is a character key code in the buffer), and false otherwise.

If you are up to it, I would suggest following LB's approach.


Psuedo Code:
char ch;
while(time != time_up)
    //update the time here... etc...
        ch = getch();
//user pressed nothing, return nothing, or somthing 

that's a rough outline, but you get the picture.
Last edited on
Topic archived. No new replies allowed.