Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++: Creating a class for a stopwatch

  1. Apr 8, 2014 #1
    Today I wanted to use my Windows phone to time myself doing a workout. After realizing that my phone doesn't have a stopwatch, I thought, "Oh, I'll make one." I've never made a Windows phone app before, but Microsoft has some documentation that should guide me later: http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj681687(v=vs.105).aspx.

    I'd like to do this as correctly as possible. There will be no fancy algorithms involved, which should make it easier to focus on straightforward implementation.

    I'm sure you've all used a stopwatch. A stopwatch has two buttons, a left one and a right one. You push the right one to first get the clock running.

    While the clock is running, if you push the right button, you pause the clock, and if you push the left button, you put the stopwatch into the next "lap", i.e. it saves the the time up until now in a previous lap. The number of laps is indefinite.

    While the clock is paused, if you push the right button, you start the clock again, and if you push the left button, you've made the current lap your final lap and officially ended your workout/race/whatever.

    Now, here's what my programming intuition tells me:

    • This should be a class. A stopwatch, as I've described it, is an object that gets instantiated. So the first thing I should do is write class Stopwatch in Notepad.
    • A stopwatch object is in one of 3 states: it's either unstarted, timing a lap or paused on a lap. So I should have a private variable clock_mode cur_mode; where clock_mode is an enumerator enum clock_mode {unstarted, running, paused};
    • There should be 2 and only 2 public functions: void start() and void stop(). The inner workings of this object will subsequently be called by those functions.
    • The laps need to be stored in a linked list or similar structure. Each new lap gets pushed back onto the structure. This calls for a vector, according to Bjarne Stroustrop: .
    • I will be needing to bring in <ctime.h> or whichever library is the one for time.

    Any thoughts so far? I need help gaining a better programming intuition.
     
    Last edited by a moderator: Sep 25, 2014
  2. jcsd
  3. Apr 8, 2014 #2
    Hopefully this shows you what my gameplan looks like:

    Code (Text):

    #include <vector>

    class Stopwatch
    {
        private:
        enum clock_mode {unstarted, running, paused};
        clock_mode cur_mode;
        struct lap {int lap_num; double time;};
        int lap_count;
        std::vector<lap> laps_vec;

        public:
        Stopwatch();
        ~Stopwatch();
        void right_button();
        void left_button();
    };

    Stopwatch::Stopwatch()
    {
        cur_mode = unstarted;
        lap_count = 0;
    }

    Stopwatch::~Stopwatch()
    {
        // trivial destructor
    }

    void Stopwatch::right_button()
    {
         switch (cur_mode)
         {
            case unstarted:
                // start lap 1
                ;
            case running:
                // pause
                ;
            case paused:
                // start running again
                ;

         }
    }

    void Stopwatch::left_button()
    {
        switch (cur_mode)
        {
            case running:
                // advance to the next lap
                ;
            case paused:
                // reset
                ;
        {
    }
     
     
  4. Apr 9, 2014 #3
    there is a stopwatch class in he .net framewrok. c++ is on the .net framework. system.diagnostics.stopwatch

    https://code.google.com/p/cpp-stopwatch/

    not to be opprobrious but isn't asking us to code for you kinda against the rules?
     
  5. Apr 9, 2014 #4
    When did I ask you to code for me?

    And what rules are you talking about?
     
  6. Apr 17, 2014 #5
    What is the difference between "unstarted" and "paused"? Clue: the difference is the value of an unrelated elapsed time counter. So, you need only 2 states in your enum.

    Your public functions (API) should represent the buttons. That is what a real stopwatch exposes. So your API should expose 2 functions: leftbutton and rightbutton (usually named start/stop and lap on a real device). Otherwise, your API will be unintuitive and anyone using your code will be sad, and whine on proggit about poor API design in modern software.

    I don't know what time library you need for a windows phone. Getting time right is one of those platform-specific things and I don't use the Windows platforms. Whatever you pick, you should ensure it has a high resolution clock. You might time the task directly, or ask the operating system to time it for you. I would recommend the latter.
     
  7. May 12, 2014 #6
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++: Creating a class for a stopwatch
  1. Creating a class (Replies: 2)

Loading...