Any possible way to create a timer to C++ program?

I was going to make a game with C++, and I want´d a timer to it.
Is there any way to create a timer? (Like 2 functions, which start and stop the timer.) Oh, and it would be better, if the timer would be only in seconds, no minutes or hours counted. (Once you reach 60 seconds, it doesn´t say you have 00:01:00 of time, so I can use it with the if, else and switch "things". (Dunno the right word.)
try this

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
#include <iostream.h>
#include <time.h>


class timer {
	private:
		unsigned long begTime;
	public:
		void start() {
			begTime = clock();
		}

		unsigned long elapsedTime() {
			return ((unsigned long) clock() - begTime) / CLOCKS_PER_SEC;
		}

		bool isTimeout(unsigned long seconds) {
			return seconds >= elapsedTime();
		}
};


void main() {
	unsigned long seconds = 10;
	timer t;
	t.start();
	cout << "timer started . . ." << endl;
	while(true) {
		if(t.elapsedTime() >= seconds) {
			break;
		}
		else {
			// do other things
		}
	}
	cout << seconds <<  " seconds elapsed" << endl;

	cin >> seconds;	// it's just to stop the execution and look at the output
}


You can add methods to the class timer to better fit your game requirements.
I hope this can help you!
I just extended my previous code to make the class timer work like a chronograph.
I hope this is useful for your game!

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream.h>
#include <conio.h>
#include <time.h>	// class needs this inclusion


//////////////////////////////////////////
// class declaration:


class timer {
	public:
		timer();
		void           start();
		void           stop();
		void           reset();
		bool           isRunning();
		unsigned long  getTime();
		bool           isOver(unsigned long seconds);
	private:
		bool           resetted;
		bool           running;
		unsigned long  beg;
		unsigned long  end;
};


//////////////////////////////////////////
// class implementation:


timer::timer() {
	resetted = true;
	running = false;
	beg = 0;
	end = 0;
}


void timer::start() {
	if(! running) {
		if(resetted)
			beg = (unsigned long) clock();
		else
			beg -= end - (unsigned long) clock();
		running = true;
		resetted = false;
	}
}


void timer::stop() {
	if(running) {
		end = (unsigned long) clock();
		running = false;
	}
}


void timer::reset() {
	bool wereRunning = running;
	if(wereRunning)
		stop();
	resetted = true;
	beg = 0;
	end = 0;
	if(wereRunning)
		start();
}


bool timer::isRunning() {
	return running;
}


unsigned long timer::getTime() {
	if(running)
		return ((unsigned long) clock() - beg) / CLOCKS_PER_SEC;
	else
		return end - beg;
}


bool timer::isOver(unsigned long seconds) {
	return seconds >= getTime();
}


//////////////////////////////////////////
// class test program:


void main() {
	bool quit = false;
	char choice;
	timer t;
	while(! quit) {
		cout << " s   start/stop " << endl;
		cout << " r   reset" << endl;
		cout << " v   view time" << endl;
		cout << " q   quit" << endl;
		cout << endl;
		choice = getch();
		switch(choice) {
			case 's':
				if(t.isRunning()) {
					t.stop();
					cout << "stopped" << endl;
				}
				else {
					t.start();
					cout << "started" << endl;
				}
				break;
			case 'r':
				t.reset();
				cout << "resetted" << endl;
				break;
			case 'v':
				cout << "time = " << t.getTime() << " ms" << endl;
				break;
			case 'q':
				quit = true;
				break;
		}
		cout << "------------------------------" << endl;
	}
}

Thanks! Iv´l post that game, once it´s finished. :)
Last edited on
oh yes please :)
Well...
When I´m trying to create it, it gaves this kind of errors:

1
2
3
error C2065: 't' : undeclared identifier
Game.cpperror C2228: left of '.start' must have class/struct/union type
error C2228: left of '.getTime' must have class/struct/union type


Any idea?
Last edited on
you can declare a timer before your main function so you can see the timer globally and then you start it in the main function.
I mean something like this:

1
2
3
4
5
6
7
8
timer t;

// write here other functions that need timer visibility

int main() {
t.start();
// write here other code
}

I hope you understand what i mean.
Declaring the timer as a global object you can see it in the main function and also in the other functions.
Last edited on
Lol, damn me! I didn´t read the "timer t;" part of your code, when I was making my own. Thanks anyway!
:) no problem
Anyway if you declare "timer t;" inside the main function body is a better and cleaner solution then the last one I told you.
Otto I found an arror in my code:

I wrote:
1
2
3
4
5
6
unsigned long timer::getTime() {
	if(running)
		return ((unsigned long) clock() - beg) / CLOCKS_PER_SEC;
	else
		return end - beg;
}


that was wrong because if the timer is running returns a time interval in seconds but if the timer is not running it returns a time interval in milliseconds.

The rigth code is:

1
2
3
4
5
6
unsigned long timer::getTime() {
	if(running)
		return ((unsigned long) clock() - beg);
	else
		return end - beg;
}


that return a time interval in milliseconds
or if you want it in seconds the code is the following

1
2
3
4
5
6
unsigned long timer::getTime() {
	if(running)
		return ((unsigned long) clock() - beg) / CLOCKS_PER_SEC;
	else
		return (end - beg) / CLOCKS_PER_SEC;
}

Last edited on
Topic archived. No new replies allowed.