Why isn't my timer working?

This is a discussion on Why isn't my timer working? within the C++ Programming forums, part of the General Programming Boards category; Hello I made a simple timer that should work, but doesn't. I keep getting 1 second as the time. Even ...

  1. #1
    Registered User darknite135's Avatar
    Join Date
    Dec 2007
    Posts
    16

    Smile Why isn't my timer working?

    Hello I made a simple timer that should work, but doesn't. I keep getting 1 second as the time. Even if I leave it on for a minute, it said it took 1 second. Have a look at my code.
    Code:
    #include <iostream>
    #include <time.h>
    using namespace std;
    
    int main()
    {
        time_t start, end;
        double timediff;
        char answer, answer2;
        
        while(1)
        {
        cout << "Welcome to the timer, type s to start and stop time\n";
        cin >> answer;
        if (answer == 's')
        {
        time (&start);
        cout << "Timer started";
        cin >> answer2;
        
        }
        
        if (answer2 == 's')
        {
        time(&end);
        cout << "Timer ended\n";
        timediff = difftime(end, start);
        cout << "That took " << difftime << " seconds\n";        
        }
        
        
        }
        
        system("PAUSE");
        return 0;
    }
    Can anyone help me? Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Code:
    cout << "That took " << difftime << " seconds\n";
    I think that should be timediff

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    756
    Answer and answer2 should be different also.
    answer2 == 'f'
    Code:
    time_t start, end;
        double timediff;
        char answer, answer2, quit;
        
        while(1)
        {
         cout << "    \t\t\tWelcome to the timer!\n\n" 
         "\tType 's' +  enter to start and 'f' +  enter to finish time!\n\n"
         "  \t\t\tType 'q' + enter to quit!\n";
         cin >> answer;
         if (answer == 'q')
         {
           break;
         }
         if (answer == 's')
         {
          time (&start);
          cout << "Timer started...\n";
          cin >> answer2;
         }
        
         if (answer2 == 'f')
         {
          time(&end);
          cout << "Timer ended.\n";
          timediff = difftime(end, start);
          cout << "That took " << timediff << " seconds\n";
         }
        }
        return 0;
    Last edited by Ducky; 12-31-2007 at 10:48 AM.

  4. #4
    Registered User darknite135's Avatar
    Join Date
    Dec 2007
    Posts
    16
    Thanks everyone for helping me! It works now Now to see how long it takes me to write a program lol. Happy New Year!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Timer again.
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 05-04-2005, 10:19 PM
  2. Timer in Win32
    By nicolas in forum Windows Programming
    Replies: 5
    Last Post: 07-04-2004, 07:21 AM
  3. Replies: 4
    Last Post: 08-13-2003, 07:25 PM
  4. allegro timer in a class
    By MadHatter in forum Game Programming
    Replies: 7
    Last Post: 12-06-2002, 09:06 PM

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