CrazyEngineers
  • 😁
    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:
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • sriramchandrk

    MemberOct 13, 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 <stdio.h>
    #include <signal.h>
     
    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");
     }
    }
     
    
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register