How do I countdown?

I really need a timer example, that I put into it seconds and after those seconds are up the application closes. Since I don't understand and know how to do that, I tried this:

1
2
3
4
5
6
7
8
9
10
11
12
13
std::cout << "Enter Y to restart. Closing automatically in 5 seconds..." << std::endl;
       std::cin >> chrAns;

	   while (! (std::cin >> chrAns))
	   {
			for (int intStart = 0; intStart < 5000; intStart++)
			{
				if (intStart == 5000)
				{
					exit(1);
				}
			}
	   }


Can someone tell me why that won't work? Or give me a timer example PLEASE AND THANK YOU!
Last edited on
that won't work because you don't have a time delay,
so when it executes the for loop, it will loop through it 5000 times in less than a second.
Because it's not waiting for a second to pass before incrementing. so your for loop would look something like the following:

1
2
3
4
5
6
7
8
for (int intStart = 0; intStart < 5000; intStart++)
{
	if (intStart == 5000)
	{
		exit(1);
	}
        // insert time delay here like Sleep(1000);
}


the Sleep function is a time delay function where you pass in the number of milliseconds. It's included in windows.h. However you can only use that if you're using windows I think.
Last edited on
You'll need a timed poll.

Here's one for Windows: http://www.cplusplus.com/forum/beginner/5619/#msg25047
If you are on linux, you'll need something similar: http://www.cplusplus.com/forum/general/5304/#msg23940

Using Sleep() is not right because the default I/O is blocking. Once you try to get input, the computer will wait forever for the user to give input.

Good luck.
Topic archived. No new replies allowed.