Is there a way to reset the clock function?

Well the title does says clock() function. Let me be more clear,
so no wise comment is made. I have a object spinning based on
the time the user presses the right key, If the user stops pressing
the right key then I need to reset the clock() function to 0. If there
is another way please suggest.

call clock() at the start, then again at the end, and subtract the two.

clock_t t1, t2;
t1 = clock();
// do something here
t2 = clock();
clock_t diff = t2 - t1;

In general, no. But you can wrap the clock() inside a class and simulate a reset method:

class Clock
{
    private:
        clock_t naught;
    public:
        Clock():naught(0){} //constructor
        void reset()
        {
            naught=clock();
        }
        clock_t operator()()
        {
            return clock()-naught;
        }
};
//demonstration:
int main()
{
    using std::cout;using std::endl;
    Clock stopwatch;
    while(stopwatch()<2 * CLOCKS_PER_SEC);   //wait for 2 seconds
    cout<<"Our Clock()"<<stopwatch()<<"\t\t";//print our Clock()
    cout<<"Standard Clock()"<<clock()<<endl; //print the standard clock()
    
    
    stopwatch.reset();
    cout<<"\tOur Clock Reseted....\n";
    
    
    while(stopwatch()<2 * CLOCKS_PER_SEC);   //wait again
    cout<<"Our Clock()"<<stopwatch()<<"\t\t";//print our Clock()
    cout<<"Standard Clock()"<<clock()<<endl; //print the standard clock()
}

This is more or less like post#5 but in an encapsulated way.

This article has been dead for over six months. Start a new discussion instead.