We know input function or operator (cin, scanf,gets….etc) wait to take input form user & this time has no limit.
Now, I will ask a question & user give the answer, till now there no problem but my problem is “user has a time(may 30 or 40 sec) to give the input, if he fail then input statement will automatically deactivated & execute next statement.”
I think you get my problem. Then please help me in this situation. It will be better if someone give me some really working example code.
I use codebolck 12.11 in windows 7.
Another Method:
You can use POSIX select() function (and some macros
FD_ZERO
,FD_SET
,FD_ISSET
) to check which file descriptors (descriptor number0
i.e. stdin, in this case) are ready to be read in a given time interval. When they are ready, use appropriate function to read the data (scanf()
in this case).This code might help you understand, what I want to say:
More Help: select(2)
You can also try using poll() function available in (again a POSIX standard function) as an alternative to select(). See poll() & poll(2)
An approach for *IX'ish systems (including Cygwin on windows):
You could use
alarm()
to schedule aSIGALRM
, then useread(fileno(stdin), ...)
.When the signal arrives
read()
shall return with-1
and had seterrno
toEINTR
.Example:
Note: In the example above the blocking call to
read()
would be interupted on any signal arriving. The code to avoid this is left as an execise to the reader ... :-)