Thread: Timer In C

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    6

    Timer In C

    I just want to know whether anyone does know about the timer control in C, the problem is that when we use any input function like getchar(),getche() or even scanf(), then if user doesn't give the input, the program is suspended for infinite time. But is there a way to get out such that you give input only within 1 min (suppose) and if user is not giving input, the program continues with the execution of program taking user input as NULL. It may be real help for online based programs in C. Like in VB we have a Timer control which does that job. Does any guy know about it or any refrence that mentions it. Thanks For Your Patient Reading.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    5

    Thumbs up

    may be you can use kbhit()....

    Code:
    if( kbhit() )
    {
        getchar/scanf/getch
    }
    this function is run if you push some button from your keyboard...
    so if you don't push anything...
    this function is not running.....

    sory my english is not very good...
    i hope you understand what i mean....

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    _kbhit() is non-standard and you must

    Code:
    #include <conio.h>
    which is also a non-standard header.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Not sure what kind of input you want but
    Code:
    #include <conio.h>
    #include <time.h>
    
    int t = time(NULL);
    char c;
    while (time(NULL) - t < 60) {
        if (kbhit()) {
            putchar(c = getch());
            if (c == '\n')
                break;
        }
    }
    might be a start.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Hi!

    Maybe it is irrelevant in the context but I used the time() function in one of my programs (which was web based) to calculate the typing speed of an individual for advanced security. The results were accurate. But keep in mind that in this case there are too many function calls for a single input in the worst case.

    Regards,

    Arun

  6. #6
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Well, the best way of doing this is to use your OS internal timers. If you are using a UNIX based system you can use any one the ITIMER_* variables. Set the values of your ITIMER_* variables (say 1 min) . And at the begining of your program you can catch a signal raised from the OS upon the expiration of your timer. Your code should look like something below
    Code:
    int main() {
    .....
    struct itimerval sth;
    ....
    ....
    signal(SIGALRM,SIG_DFL);
    setitimer(sth....);
    ....
    scanf()/fgets() whatever...
    ...
    return o;
    }
    In Windows environmet you can also use such a mechanish (timers). You should check MSDN for detailed information if you use Windows as development environment.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    Cool

    Hi!

    Nice solution. Hats off!

    Regards,

    Arun

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by OnionKnight
    Not sure what kind of input you want but
    Code:
    #include <conio.h>
    #include <time.h>
    
    int t = time(NULL);
    char c;
    while (time(NULL) - t < 60) {
        if (kbhit()) {
            putchar(c = getch());
            if (c == '\n')
                break;
        }
    }
    might be a start.
    There's a nice function called getche() that does exactly what this line does:
    putch(getch());

    Also with getch(), return on your key board gives a carriage return, not a new line. Control return gives a new line, I believe.

    Furthermore, getch() return the character alpha when a non letter character is hit. A subsequent call to getch() would then return a letter character representing which character was hit.
    Last edited by King Mir; 04-20-2006 at 12:23 PM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The whole question is unanswerable unless unmeshghosh states the OS and compiler being used.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    6
    Thanks you all for paying such attention by the way the operating system i am talkin is Fedora Core 3 and compiler is gcc.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Scroll to the end
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Also, read up on termios as well, it will allow you to set timeouts as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SIGALRM and timer
    By nkhambal in forum C Programming
    Replies: 1
    Last Post: 06-30-2008, 12:23 AM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Need help with a count down timer
    By GUIPenguin in forum C# Programming
    Replies: 0
    Last Post: 07-07-2006, 04:18 PM
  4. Timer again.
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 05-04-2005, 10:19 PM