Error(s) found: '1'

  • Unable to perform the query 'UPDATE cpp_code SET clickthru=clickthru+1 WHERE fid=6'. Table 'cpp_code' is read only.

  • C++ MADE EASY
    Code About Us Tutorial




       
     

    Home > myCode > A Basic Timer Class

    November 29, 2009 3:46 pm

       

    A Basic Timer Class


    Posted on: 2001-09-15 10:15:46
    Number of times viewed: 752

     
     
    //ctimer.h
    // MoMad
    #include <time.h>
    
    class CTimer
    {
    private:
    	time_t m_current_time;
    	time_t m_seconds_elapsed;
    	time_t m_start_time;
    	
    	bool m_paused;
    	bool m_is_running;
    
    public:
    	CTimer();
    	void reset();
    	void start();
    	void stop();
    	unsigned long getElapsedSecs();
    };
    
    // Mohamed Nuur
    //ctimer.cpp
    
    #include "ctimer.h"
    
    CTimer::CTimer()
    {
    	m_paused = false;
    	m_is_running = false;
    	m_seconds_elapsed = 0;
    	m_start_time = 0;
    	m_current_time = 0;
    }
    
    void CTimer::start()
    {
    	if( !m_is_running )
    	{
    		m_is_running = true;
    		m_start_time = time(0);
    	}
    }
    
    void CTimer::reset()
    {
    	if( !m_is_running )
    	{
    		//reset the timer.
    		m_start_time = time(0);
    		m_seconds_elapsed = 0;
    	}
    	else
    	{
    		//stop then reset then start again.
    		stop();
    		reset();
    		start();
    	}
    }
    
    void CTimer::stop()
    {
    	if( m_is_running )
    	{
    		if(m_start_time == 0)
    		{
    			m_start_time = time(0);
    		}
    		
    		//stop the time, add elapsed time, reset the first flag
    		m_current_time = time(0);
    		m_seconds_elapsed += (m_current_time - m_start_time);
    		m_start_time = time(0);
    		m_is_running = false;
    	}
    }
    
    unsigned long CTimer::getElapsedSecs()
    {
    	if( m_is_running )
    	{
    		//Note: stop() updates the elapsed seconds.
    		stop();
    		start();
    	}
    
    	return m_seconds_elapsed;
    }

    Please Rate this Code:



     «12039» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.