Wait until user key or timeout

Nov 22, 2010 at 2:55pm

Hello,
I know I can use the functions sleep() or wait() for stopping execution for a given amount of time or until the user presses any key.

But what I want to do is different: I'd like to stop the execution until the user inputs an integer value (which I could simply do with cin), but if the user fails to do so (for example, he's not at the computer) for more than n seconds, the execution should continue assuming a default integer value.

Does anybody know an easy way to have this working?
Thanks a lot,
cheers
Francesco
Nov 22, 2010 at 4:17pm
only threads, I suppose.
Nov 23, 2010 at 10:28pm
Nov 23, 2010 at 11:32pm
I'm trying to keep a cross-platform program and I had already worked this out under windows using kbhit, now I'm going to try also the linux solution you posted...
so, thanks a lot!
Francesco
Nov 24, 2010 at 12:36am
JSYK, kbhit() is not cross-platform even on Windows...

Doing this kind of thing is always platform-dependent. The best that can happen is that you find a library that encapsulates these dependencies for you... Like NCurses.
Nov 24, 2010 at 10:15am
ah ok, thanks for the info, I thought I could use kbhit on windows and another similar function on linux with some #ifdef to switch. I'll look into NCurses, Francesco
Nov 24, 2010 at 4:33pm
please check this out for unix/linux!

---
http://www.unix.com/programming/16678-how-get-timed-input-using-cin.html
---

Regards,
Venu Yanamandra
Topic archived. No new replies allowed.