5 Replies - 284 Views - Last Post: 28 March 2011 - 10:23 AM Rate Topic: -----

#1 muffinman8641  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 121
  • Joined: 17-March 11

Timer issue (want it to be more specific than 1 second)

Posted 25 March 2011 - 08:13 AM

I'm making a minigame where the user is shown a letter and has to enter it as fast as possible. This is (some of) the code:
start = time(NULL);
    cout << "w\n";
    cin >> letter;
    end = time(NULL);


It only measures down to a whole second, though; I want it to be as specific as 0.01 seconds. How can I do this?
Is This A Good Question/Topic? 0
  • +

Replies To: Timer issue (want it to be more specific than 1 second)

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Timer issue (want it to be more specific than 1 second)

Posted 25 March 2011 - 08:31 AM

You can use
    clock_t start, end;
    start = clock();
    cout << "w\n";
    cin >> letter;
    end = clock();


and use the difference between start and end to calculate the number of system clock ticks that elapsed.

Then you can use CLOCKS_PER_SEC to convert that to a fraction of a second.

Here's an example.

This post has been edited by r.stiltskin: 25 March 2011 - 08:32 AM

Was This Post Helpful? 1
  • +
  • -

#3 muffinman8641  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 121
  • Joined: 17-March 11

Re: Timer issue (want it to be more specific than 1 second)

Posted 28 March 2011 - 08:14 AM

I made the variable declaration look like this:

 clock_t start = (clock() + (0.1 * CLOCKS_PER_SEC) / 1000)


but even though I have the /1000 there it returns, for example, 1500 seconds instead of 1.5 seconds. Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Timer issue (want it to be more specific than 1 second)

Posted 28 March 2011 - 08:24 AM

If start is the initial system time, and end is the ending time:
double elapsed = (double)(end - start)/CLOCKS_PER_SEC;
should give you the number of seconds.
Was This Post Helpful? 0
  • +
  • -

#5 muffinman8641  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 121
  • Joined: 17-March 11

Re: Timer issue (want it to be more specific than 1 second)

Posted 28 March 2011 - 08:29 AM

I put it in and replaced all the (end - start) with "elapsed", but now it gives me -198565e+006 seconds... :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Timer issue (want it to be more specific than 1 second)

Posted 28 March 2011 - 10:23 AM

You're doing something wrong (can't tell what without seeing the actual code) but it doesn't matter. I just realized that clock() won't do what you want anyway -- the program has to actually be doing something to use clock(). If it's just sitting idle waiting for input, clock() will just show 0.

Assuming you're working in Windows, you can use something like this:
#include <iostream>
#include <windows.h>
using namespace std;


int main() {
    DWORD timestart, timeend;
    timestart = GetTickCount();
    std::cout << "counting time until you press enter\n";
    while(true) {
        if( cin.ignore() ) {
            break;
        }
    }
    timeend = GetTickCount();
    cout << "elapsed time: " << (double)(timeend-timestart)/1000 << " seconds" << endl;
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1