Problem with Sleep() when drawing an analog clock

Hi,

The question says:

Make an "analog clock" that is, a clock with hands that move. You get the time of day from the operating system through a library call. A major part of this exercise is to find the functions that give you the time of day and a way of waiting for a short period of time (e.g., a second for a clock tick) and to learn to use them based on the documentation you found. Hint: clock(), sleep().

OK, I wrote below code. It is in its primary stages and has not been completed yet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <GUI.h>
#include <time.h>
#include <iostream>
using namespace Graph_lib;

//---------------------------------

class Dynamic_clock : public Window {

public:
	Dynamic_clock(Point p, int w, int h, const string& title):
		Window(p, w, h, title),
		quit_button(Point(x_max()-90,20), 60, 20, "Quit", cb_quit),
		l1(Point (p.x+100,p.y), Point(p.x+120,p.y)),
        l2(Point (p.x-100,p.y), Point(p.x-120,p.y)),
		l3(Point (p.x,p.y-100), Point(p.x,p.y-120)),
		l4(Point (p.x,p.y+100), Point(p.x,p.y+120)),
		hour1(Point(300,250), Point(300,155)),
		hour2(Point(300,250), Point(310,165)),
	    c(Point(p), 120) {
			attach(c);
			attach(l1);
			attach(l2);
			attach(l3);
			attach(l4);
			attach(quit_button);	
			clock_hands();
	}

private:
	Button quit_button;
	Circle c;
	Line l1, l2, l3, l4, hour1, hour2;
	double h, m, s;

	void quit() { hide(); }

	void clock_hands() {
		get_current_time();
		hour1.set_style(Graph_lib::Line_style(Graph_lib::Line_style::solid, 3));
		attach(hour1);
		Sleep(1000);
	        detach(hour1);
		attach(hour2);
		
	}
	void get_current_time() {
		time_t t = time(0);
		t -= 1421526600;
		double h = t/3600;
		t -= h*3600;
		double m = t/60;
		t -= m*60;
	    double s = t;
	}


	static void cb_quit(Address, Address pw) {reference_to<Dynamic_clock>(pw).quit();}
};

//------------------

int main() {
		Dynamic_clock dc(Point(300,250), 800, 600, "Dynamic_clock"); 
		return gui_main();
}


Now what is the problem?

I expect the system in void clock_hands() (line 38) attaches hour1 (line 41) then waits for 1000 ms (using Sleep(1000)) then detaches hour1 and attaches hour2 this time. But it doesn't occur in practice. When the system reaches Sleep(1000); it seems to go into a comma! It doesn't show the hour1 so seeing the movement of clock ticks by the clock's hands will not be possible.

Any idea please?
Last edited on
Presumably, get_current_time should have some side effect. It would have the same effect if the body of the function were empty.
I removed calling of get_current_time(); (line 39) from clock_hands() but no changes in result!
Isn't there a way? Regardless of my code, how you would solve the problem if you were me?
The problem is an exercise. #6
http://books.google.com/books?id=We21AwAAQBAJ&lpg=PP1&dq=programming%20principle%20and%20practice&pg=PA579#v=onepage&q&f=true
Isn't there a way? Regardless of my code, how you would solve the problem if you were me?

I have no desire to learn how to use the graphics library provided for you. From what I can see of your code, you never update the variables which dictate what would be displayed. I don't find it surprising, then, that your program seems to go into an unchanging state.

Nor do I find it surprising that when you remove a call to a function that does nothing, nothing happens. My assumption in pointing out that get_current_time should have side effect, but does not, was that you would change it to have one. Preferably the side effect of updating the class variables to reflect the current time.
I don't understand what you mean!
Topic archived. No new replies allowed.