clock like cout statement

Hard to explain my issue in the title but, I am trying to understand the clock in terms of c++. A cout statement with endl statement essentially does this

00:00
00:01
00:02


However, if a clock actually did that on our computer we be pretty annoyed so in stead it replaces the particular value with something else. So ideally it only produces one output statement. So how best to implement this idea? I not even sure where to start...
Last edited on
closed account (Dy7SLyTq)
well how (ummm i guess retro fits) retro do you want to get? because i could write this with ncurses and ctime
The way I set mine up is essentially the print function takes two pointers which are pointed a separate struct. One struct for keeping tabs on the hours the other tabbing minutes. Then building functions to add to these values (and of coarse resetting when appropriate). Maybe not the most efficient way but I would like the explanation to keep that in context.


Or would this even make sense? to do it that way.
Last edited on
closed account (18hRX9L8)
Using GotoXY, we can set the cursor to what it was before and print the new clock over our old one.

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
67
68
69
70
#include <cstdio>
#include <windows.h>

/*
 * Function: GotoXY
 * Usage: GotoXY(x,y)
 * --------------------------------------------------------------
 * The GotoXY function moves the cursor to the point (x,y).
 */

void GotoXY(unsigned x,unsigned y)  {
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD CursorPosition;
	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition); 
}

/*
 * Function: PrintClock
 * Usage: PrintClock()
 * --------------------------------------------------------------
 * The PrintClock functions prints out a clock in hh:mm::ss time.
 */
 
void PrintClock(void) {
	while(true) {
		for(int hh = 0; hh < 24; hh++) {
			for(int mm = 0; mm < 60; mm++) {
				for(int ss = 0; ss < 60; ss++) {
					GotoXY(0,0);
					
					if(hh < 10) {
						printf("0%d:",hh);
					} else {
						printf("%d:",hh);
					}
					
					if(mm < 10) {
						printf("0%d:",mm);
					} else {
						printf("%d:",mm);
					}
					
					if(ss < 10) {
						printf("0%d",ss);
					} else {
						printf("%d",ss);
					}
					
					Sleep(1000);
				}
			}
		}
	}
}

/*
 * Function: main
 * Usage: CMDLINE
 * --------------------------------------------------------------
 * The main function.
 */

int main(void) {
	PrintClock();
	
	getchar();
	return(0);
}


00:00:00
Last edited on
awesome thanks
Topic archived. No new replies allowed.