countdown need help

hi im new to c++ and have just built this count down (see below) and i was wondering does any one know how to add a 1 second pause between each value been displayed so in effect it will count down in seconds


#include <iostream>
using namespace std;
int main ()
{
int n;
int a;
cout << "Enter the starting number >";
cin >> n;
while (n>0) {
cout << n << ", ";
--n;
}
cout << "\n Countdown ended\n";
cout << "\n\n";
cout << "\n Press any key then enter to exit\n";
std::cin >> a;
return 0;
}
Last edited on
You can include <ctime>

To use it:

1
2
3
4
5
 float secs = 1.2;
 clock_t delay = clock_t(secs * CLOCKS_PER_SEC);
 clock_t start = clock();
 while (clock() - start < delay );
//Then do what you want to  do 
thanks AdventWolf
No problem, I would put the code into a function so instead of including all of those lines, you can just have..

1
2
3
4
5
6
void Delay( const float secs )
{
    clock_t delay = clock_t(secs * CLOCKS_PER_SEC);
    clock_t start = clock();
    while (clock() - start < delay );
}


So then you would just have to do:

Delay(1);
Topic archived. No new replies allowed.