Thread: How would I make a stopwatch program?

  1. #1
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32

    How would I make a stopwatch program?

    I was wondering what method I would use to count upwards in seconds instead of near instantly.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well I assume you just want to add a delay to your program correct? Well I would suggest this. Add the header windows.h to your code. Then also add the library winmm.lib to your project. Use the function timeGetTime( void ), which returns a DWORD, the number or milli-seconds since windows has started. Just keep track of them and display a number every 1 second or something might look like this. The following code will print "5" every second. This is just a simple snippet to help you, if you have any questions shoot.


    int main( void )
    {
    UINT uCount = 0;
    BOOL bDone = FALSE;
    DWORD dwTime1 = 0, dwTime2 = 0;
    dwTime1 = timeGetTime();

    while( !bDone )
    {
    if( dwTime2 >= 1000 ) /* For every 1 second */
    {
    cout << " 5 " << endl;
    dwTime1 = timeGetTime();
    dwTime2 = 0;
    uCount++;
    if( uCount == 5 )
    bDone = TRUE;
    }
    else
    {
    dwTime2 = timeGetTime() - dwTime1;
    }
    }
    return 0;
    }

  3. #3
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    ..or you could you use the time.h header file.

    it has a difftime() to calc differences [] in two times.
    -

  4. #4
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    How would a program using that look?

  5. #5
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    the best things in life are simple.

  6. #6
    Unregistered
    Guest
    // This will output the elasped time
    // I used system("pause") to create
    // the interval cin works as well


    #include<iostream>
    using std::cout;
    #include<ctime>


    main()
    {
    time_t start, end;
    cout <<"Start\n";
    start = clock();
    system("pause"); // Wait for a keypress
    end = clock();
    cout <<"Done\n";
    cout << "The interval was : " << (end-start) << " ms.\n";
    return (0);
    }

  7. #7
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    Why does the program take a second to react (making the total time more than it should)

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    23
    Originally posted by marcusbankuti
    Why does the program take a second to react (making the total time more than it should)
    I'm assuming that you would need to do some sort of threading if you want real time stopping (stopping the watch at exactly the time you hit the key). Unfortunately I have never messed with threading in C++, only in Java.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stopwatch or Timer in a simple C program
    By DaniiChris in forum C Programming
    Replies: 6
    Last Post: 07-10-2008, 02:28 AM
  2. Replies: 9
    Last Post: 06-17-2008, 11:38 AM
  3. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 02:05 AM
  4. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM
  5. any suggestions on how to make this program smaller?
    By bajan_elf in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2003, 03:24 AM