Teach a child about an analog clock

Your goal in this lab is to teach a child to use an analog clock. The idea is to display the time in digital form, and ask the child to click on where the hour hand should point to, and then to where the minute hand should point.

  1. Change your clock program from last lab, and so that it draws the face and tick-marks, but doesn't draw the clock hands.

  2. Somewhere in the window (perhaps beneath the clock) display the current time in digital form in normal (non-military) format with am or pm. For example, 1:32pm.

    We haven't taught you a way to combine an integer and a string. Include the procedure digitToString before your main():

    #include <string>
    string digitToString (int digit) {
       assert (digit >= 0 && digit <= 9);
       static string dummy = "X";
       dummy[0] = '0' + digit;
       return dummy;
    You should now be able to type:
       string five = digitToString (0) + digitToString(5);
       cout << five << endl;
    Hint: You'll want to use n % 10 and n / 10 to extract individual digits.

  3. Be sure to test your code so far by using both a morning and afternoon time. The best way to do this is in gdb, since gdb allows you to change the values of variables while running a program, without changing the program! For your convenience using gdb gives you the documentation forgdb. (Remember that the bottom of this linked file gives you the emacs command for setting breakpoints.) To do the debugging:
    1. Run gdb
    2. Set a breakpoint immediately after the current time, say it's called now, is declared.
    3. Run your program in gdb
    4. Change the value of now by using gdb's set command.
        set expression
      For expression, you can use any C++ expression, but you may need to be specific about adding the class name to a method. For example, in some debuggers you must type,
        print now.Time::get_hours()

      In this case, a useful expression would add, say, 60 * 60 * 12 seconds to the time to make your program think its 12 hours later. (If you have troubles doing this, ask!)

    This technique is a good one for debugging code, since you don't have to actually change the code to test it. Were you to change the code to (temporarily) hardwire a time, you might forget to fix it again later.

  4. Check-off (1 point): Demonstrate that you've done the set in gdb to test the am/pm part of your code.

  5. Display a message in the window, asking the user to click on where the hour hand should point. If the user clicks outside the clock circle, or too close to the center of the clock, output an appropriate message and exit.

    For this part, you need to learn how to use the inverse of the tan in C++. There are two functions which might come in handy, which I found by the man command in Linux (or M-x man in emacs). I typed

      man -k atan
    to get a list of all manual pages which mention the atan function. There are two that are relevant, atan and atan2. Before embarking on the program, be sure to figure out which of the two is more useful. Inspect the individual manual pages by typing:
      man atan
      man atan2

  6. Draw the hour hand pointing in the direction the user clicked. The hour hand should be of fixed length, not the length suggested by the mouse click.

  7. Repeat for the minute hand.

  8. If the hands are pretty close to where they should be, congratulate the user.

  9. Check-off (2 points): Once done, be sure to get checked off. As always, we'll check that you have clean code.