time based entry

i want to do a time based entry like the user should be allowed to enter only in the given time say 10 sec and if they enters anything within the time it will be accepted otherwise not...we can do this using threads but i am not able to figure out how...can someone help me with this?
You don't need threads. Just get the current time before you prompt for input. Immediately after they give the input, get the current time again and subtract the two values. If more than 10 seconds has elapsed, reject the input.

You can probably use time time() function to get the current time.
To write the code no, problems yes

Basic, tutorial section of this site
Threads and thread synchronization http://www.cplusplus.com/reference/mutex/mutex/
Time http://www.cplusplus.com/reference/chrono/
I assume this is the console, yes?

My first instinct is to say "Don't do this. The console is not designed for this kind of thing."


That said... the big problem with standard IO is that it's blocking. This means when you use cin to get some input from the user... your code is blocked (it does not execute) until the user inputs something.

For you to accomplish what you want, you will need to use a non-blocking input method. The standard library does not provide any non-blocking input methods, so you'll have to use other libs.

The most available is probably kbhit() and getch() from <conio.h>. While not part of the standard lib, it is widely available so you probably have it on your machine.

I never use it myself... but I found this example from google:
http://stackoverflow.com/questions/15603082/how-to-use-kbhit-and-getch-c-programming

The thing to note here is that kbhit is non-blocking which means if the user did not input anything, kbhit will not wait around like cin will. It will return immediately.

This will allow you to run other code (like having a timer count up to 10 seconds) while you wait for the user to input something.



EDIT:

2x ninjas

dhayden's approach is much simpler than mine and will probably work great for your purposes. The key difference between his approach and mine is that his approach will still wait around forever for the user to input something... but will reject it if they did not provide it in time. Whereas my approach would give you the ability to actually interrupt and stop waiting around once time has expired.

I do not recommend threads for this. Having another thread doesn't help much when your input is blocking... because the only way to unblock it would be to forcibly kill the thread... which is not advised.
Last edited on
dhayden's approach is simple as Disch said and has problem as Disch said.

The conio/kbhit approach will only work on Windows (read http://en.wikipedia.org/wiki/Conio.h) if it is a console application.

For UNIX/POSIX non-blocking I/O will need to do something like

1
2
3
4
5
6
7
8
#include <unistd.h>
#include <fcntl.h>

const int fd = fileno(stdin);
const int fcflags = fcntl(fd,F_GETFL);
if (fcflags<0) { ... /* handle error */}
else
if (fcntl(fd,F_SETFL,fcflags | O_NONBLOCK) <0) { /* handle error */}


This is a crude code, and also probably is advanced for you to try so I guess you have only dhayden's approach unless you are on Windows.
my purpose would be fulfilled by Disch's method...thanks..i ll try and get back to you..
Topic archived. No new replies allowed.