how to do a stopwatch in a C++ program

This is a discussion on how to do a stopwatch in a C++ program within the C++ Programming forums, part of the General Programming Boards category; hi how do you make it so it does like here it starts the stop watch and then runs whatever ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    37

    how to do a stopwatch in a C++ program

    hi how do you make it so it does like here it starts the stop watch and then runs whatever code then you stop the timer. I know how to find it out if you have a window open but how do you do it with the main thing.

    thanks in advanced,
    h3ckf1r3

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Is this like what you want?
    Code:
    #include <iostream>
    #include <ctime>
    
    int main()
    {
        unsigned int start = clock();
        std::cout << "waiting for keyhit";
        std::cin.ignore();    
        std::cout << "Time taken in millisecs: " << clock()-start;
        std::cin.ignore();   
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    use gettimeofday() (portable, recommended) or some Win32 API function to get higher precision.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    thanks this is really helpful and cyberfish I am sure the commands you are talking about you would use the same way as mike_g's idea (wich by the way is really goodand I am thankful for it) you would make a variable the current time and then later on print the the new current time - the variable with the old time. I am yet to try it with cyberfish's method and I will try it in a moment.

    thanks to all,
    h3ckf1r3

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    note that the clock() function mike_g suggested is implementation specific. It can either measure real time (wall time) or CPU time. If I remember correctly, on Windows it measures real time, while on Linux it measures CPU time (don't know what happens on the Mac, but I would guess it's going to be the same as Linux since they are both POSIX and both use gcc). Therefore, it is ambiguous and I recommend against it.

    See
    http://www.thescripts.com/forum/thread517364.html

    as for the timing method, yes I would do it the way mike_g suggested.
    Last edited by cyberfish; 01-05-2008 at 03:10 PM.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    well I tried to use yours and just used mik_g's program exept I just changed the clock() function for the timeofday() function and it didnt work. Do you have to use it differently or something?

    thanks for all help,
    h3ckf1r3

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    yes you have to use it differently. Google is your friend.
    http://www.google.ca/search?hl=en&q=...=Google+Search

    here is my "adapter function" that makes it behave like clock() or time()
    Code:
    double current_time () { //returns current time in seconds
    	timeval tv;
    	gettimeofday(&tv, NULL);
    	double rtn_value = (double) tv.tv_usec;
    	rtn_value /= 1e6;
    	rtn_value += (double) tv.tv_sec;
    	return rtn_value;
    }
    make sure you include appropriate headers. #include <sys/time.h> IIRC.

    note that it returns a double.

    not rocket science, probably not even terribly optimized code, but works well enough for me.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    1
    I hav a longer but neater form.Sorry i'm an elementary programer
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<dos.h>
    #include<process.h>
    #include<iomanip.h>
    void main()
    {
    clrscr();
    int l;
    cout<<"Enter limiting time(in seconds) ";//Time to which the stopwatch works till it automatically shuts off
    cin>>l;
    cout<<"Press any key to start.  Press pause-break to stop  "<<endl;
    getch();
    int m=0,s;
      a:s=1;
      b: delay(10);
        if(s<10)
         cout<<"\b"<<s;
        else if(s<100)
         cout<<"\b\b"<<s;
        else cout<<"\b\b\b"<<s;
        s++;
    
       if(s%100==0)
        { m++;
         cout<<"\b\b\n"<<m<<"   ";
         goto a;
         }
        else if(m<l) goto b;
        else
        {
         getch();
         exit(0);
        }
     getch();
    }

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    That code is useless in its current form. A stopwatch class would be much more useful since you could plug it in at any time you want and gain stopwatch functionality.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    2 years bump.

    And the code has quite a few problems. Both stylistically and technically. You can start a new thread if you want to learn about them.

  11. #11
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Wow didn't see the bump cyberfish. Sorry about that. Closed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21