C++ Command Line Timer.

Hey All,

I am having a hard time creating a working C++ timer. I am just doing this for fun to see if I can. What it shouulld do is ask for an amount of minutes, and then cout each passing minute until the timer is up.

What it is doing though is only counting down twice and then stopping. It seems like the "endl" is not triggering the "cin.get()" like I would expect. Any advice on how I could do this better?

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
#include <iostream>
#include <ctime>

using namespace std;

void countdown (int);

int main()
{
	cout << "How many minutes until your break is over?: ";
	int minutes;
	cin >> minutes;
	countdown(minutes);
	cout << endl << endl << "COUNTDOWN IS OVER!!!!\a";
	cin.get();
}

void countdown(int n)
{
	int delay = n;
	int i = n;
	clock_t time1 = clock();
	while ((clock() - time1)/CLOCKS_PER_SEC < delay*60)
	{
		int delay2 = 60; //Delay in seconds until the next time a number is displayed on screen
		clock_t time2 = clock();
		cout << endl << i;
		i--;
		cin.get();
		while ((clock()-time2)/CLOCKS_PER_SEC < delay2);
	}
}
Last edited on
Check this out:

http://www.cplusplus.com/reference/thread/this_thread/sleep_for/

It seems like the "endl" is not triggering the "cin.get()" like I would expect.
Correct. cin and cout are two different objects that do not interfere.

On line 12: The operator>> will leave the end line character in the cin stream. That is what you get on line 29 the first but not the second time.
Topic archived. No new replies allowed.