View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12829 Members
Join this group to post and comment.
Gaurav sharma
Gaurav sharma • Oct 14, 2008

C Language Input Problem In Time Bound

😁
i want know that how can a program get time bounded input in "C Language"...
i want to ask that when a user input he has only few seconds which i will define as 3 seconds ..
if user not input within 3 second nextline should be compile....
😕
plzzz don't say me "delay" or "sleep" command can use ...
i have already try it ....
i am not understand how this work on this situation ...
plzzz send me a example....
😒
but i don't think so that this problem can be solved by the "sleep/delay" command ..
it may there is a different keyword/function /command
which can solve the problem of time bounded input in "C Language"

plzzz help me .
!@!@!@!@!@!@!@:smile:
sriramchandrk
sriramchandrk • Oct 14, 2008
Hi Gaurav,

I had given a hint to use alarm command, but looks like you dont have much time.
Hope this is useful
Please give credit to me while using this code!! ( just joking)

Thanks & Regards
Sriram

#include 
#include 
 
void catch_timeout (int sig)
{
 printf("\nSorry Timeout!!\n");
 signal(SIGALRM, catch_timeout); //reinstall singal handler
}
 
int main()
{
 unsigned u, ret;
 signal(SIGALRM, catch_timeout); //install signal handler
 
 printf("Please give value of u in 4 seconds!:");
 ret = alarm(4);
 scanf("%u", &u);
 alarm(0);// in all cases you need to reset alarm
 
 if(!ret)
 {
        printf("timeout so moving ahead withe default values for\n");
 }
 
 // second try
 printf("Please give value of u in atleast in 5 seconds!:");
 ret = alarm(5);
 scanf("%u", &u);
 alarm(0);//in all cases you need to reset alarm
 
 if(!ret)
 {
        printf("timeout again so moving ahead withe default values for\n");
 }
}
 

Share this content on your social channels -