General Question

t3h_87der's avatar

How can I reset the clock in this (C++) timer function?

Asked by t3h_87der (6points) August 22nd, 2010

Here’s the code:

——————————————————————
#ifndef TIMER
#define TIMER

#include <ctime>

bool reset = false;

class Timer {
clock_t counter;
public:
Timer(): counter(0) {};

bool elapsed(clock_t ms)
{
clock_t tick = std::clock();
if(tick – counter >= ms)
{
counter = tick;
return true;
}

return false;
}
};

#endif
————————————-

My question is how can I reset the time held in this function back to 0? This is a piece of code that my friend sent me to use for a program im working on, and it works great but i cant reset it. All answers are appreciated.

Observing members: 0 Composing members: 0

4 Answers

talljasperman's avatar

I don’t know C++ very well but “bool reset = false;” sounds like it shouldn’t be included

llewis's avatar

This is going to be a stupid question/answer for you, but – why can’t you test for the condition where you want the time to equal zero, and then set the time equal to zero at that point, I would guess right before “return false;”? (Obviously, I don’t know C++ either.)(It might not help directly, but sometimes even stupid ideas can generate good ones.)

Gamer44's avatar

This is a class, so he’s probably got this in a header file, separate from the main code. I can’t help you though, I’m not too good at using/understanding someone else’s code.

Dan337's avatar

The elapsed() function compares its argument with the difference between the current time (std::clock()) and the counter variable. What you need is a function to reset the counter variable to the current time:

void reset() { counter = std::clock(); }

Except that you already have such a function: call elapsed(0). Since the difference must be at least zero, it will set the counter variable (“counter = tick”).

Here’s some documentation for the ctime library:

http://cplusplus.com/reference/clibrary/ctime/

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther