Thread: Stopwatch or Timer in a simple C program

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    48

    Smile Stopwatch or Timer in a simple C program

    Hey Guys..Was just wondering if its possible to make a stopwatch or timer in C for example if I asked the user for a value and he took too long to enter the value the timer would be counting 5 seconds ..when 5 seconds are done...the program tells the user TIME OUT!-Game Over

    Thanks in advance for all

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not terribly easy to do. What can be done fairly easy is to take a timestamp before the question is asked, then take the time again after, and determine if it "took too long".

    To put a timeout on the input and "do something else" if there's a timeout is much more complicated, both because you have to deal with non-portable, non-standardized code, but also because you need to be careful how exactly you solve the problem.

    If you really want to do this, you will need to look at "How can I read input without the user hitting enter" in the FAQ to begin with. Then you may need to add further code to set up the timeout for it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    You asked the same question 3 days ago.

    Are you amnesic or what ? There's even answer for your question in the other thread, one (which I gave) for POSIX-compliant system, the other (which MacGyver gave) for Windows.

    What do you want to know more with this thread ? If there's other way to do it ?
    I hate real numbers.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    I know I asked the same question but,I can't find it and I think its deleted so thats why I did it again
    I am sorry for any inconvience caused!

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Could you perhaps explain what needs to be timed and why? Making input into a time puzzle isn't very user friendly, or fun, even if this is some sort of game. Some people just think or type slow.

    Still you can measure the difference between some time earlier and after they input quite simply though it isn't precise:
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main (void)
    {
        double diff = 0.0;
        time_t start;
        time_t stop;
        char buff[128];
    
        printf("Quick! Input!!!\n");
        time(&start);
        if (fgets(buff, sizeof buff, stdin) != NULL) {
            time(&stop);
            diff = difftime(stop, start);
            printf("&#37;s\n", buff);
            printf("It took you %g seconds to type that\n", diff);
        }
        return 0;
    }
    If you just need to know the performance of your program, measuring the time yourself isn't accurate, but a profiler could help you.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by DaniiChris View Post
    I know I asked the same question but,I can't find it and I think its deleted so thats why I did it again
    I am sorry for any inconvience caused!
    It hasn't been deleted, it's just been pushed off the first page of threads because so many other threads have appeared in the meantime.

    If you click on your name in this thread, it brings you to this page. From there, you can click on "Find all threads started by DaniiChris" to find all the threads you've started.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    oh! Thanks dwks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  2. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  3. Simple window program
    By baniakjr in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2006, 03:46 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. I need help on a formula for this simple program.
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2002, 10:01 PM