Thread: Displaying Time to the Nearest Tenth of a Second

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Displaying Time to the Nearest Tenth of a Second

    Hi,

    I'm trying to make this simple program that starts, and then when the user presses 1, it outputs the length of time since the program started. Basically I want it to give the time accurate to a tenth of a second, but I can only work out how to do it to the nearest second at present. Can anyone help so that it outputs something like: "Time from when the program was run: 4.3 seconds."?

    Thanks.

    Code:
    #include <ctime>
    #include <iostream>
    #include <stdio.h>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    {
        int c;
        time_t hold_time;
        hold_time = time(NULL);
    
        int time1 = hold_time;
        int time2 = hold_time+10;
    
        while (time1 < time2) 
        {
            hold_time = time(NULL);
            time1 = hold_time;
            c=kbhit();
    
            if (c != 0)
            {
                c = _getch();
                if (c == '1')
                    cout << "Time from when the program was run: " 
                         << (time1-time2) << " seconds." << endl;
            }
        }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Look into clock().

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But clock measures processor time, not wall-clock time.

    Getting sub-second accuracy for this problem needs something specific to your OS/Compiler.
    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.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Actually clock() did the job perfectly. Thanks.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bengreenwood View Post
    Actually clock() did the job perfectly. Thanks.
    Just be aware that the use of clock() for this purpose is likely to yiled the result "zero" if you use it in Linux or any other OS what clock() does what the C89 standard says that clock() should do.

    --
    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.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    EDIT: I take too long to type responses...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  2. Displaying time in a dynamic fashion..how?
    By wakish in forum C++ Programming
    Replies: 11
    Last Post: 07-06-2006, 02:52 PM
  3. displaying date and time
    By AmazingRando in forum C Programming
    Replies: 2
    Last Post: 09-08-2003, 11:47 AM
  4. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM
  5. displaying the time
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 09-07-2001, 03:43 AM