Countdown timer

I know there have been other threads on here, but some of them require the use of windows-specific libraries.

I'm trying to make a countdown timer, and want to make it so the user has 30 seconds to answer the question. After that, it will move on to the next thing.

Here is my code, but I'm not sure what it is trying to do. I was even of thinking of having the seconds blinking by using clscr();, but that's not as important as getting the functionality down first.

Any tips would be greatly appreciated.

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
#include <cstdio>
#include <ctime>
#include <iostream>
using namespace std;

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();
		}
};


int main() {
	char answer;
	char ans;
	unsigned long seconds = 5;

	timer t;
	t.start();
	cout << "You have " << seconds << " seconds to answer the question: " << endl;
	cout << "Are you ready? (y/n)" << endl;
	cin >> answer;
	while(answer == 'y' || answer == 'Y')
	{
		if(t.elapsedTime() > seconds) {
			return 0;
		}
		else if(t.elapsedTime() < seconds){
			cout << "What is the capitol of California?" << endl;
			cin >> ans;
			if(ans == 's')
				cout << "correct!" << endl;
		
		}
	}
	
}
Topic archived. No new replies allowed.