Loop in given time

How do I write a program to count from 1 to 100 in 3 seconds?? I know how to do the first part of counting 1 to 100, but got no idea how to write it to be executed in 3 seconds.
In at most 3 seconds, or exactly 3 seconds?
Exactly 3 seconds
Well, how I would do it is divide 3 by 100, that'll give a time in milliseconds, n. Iterate your loop every n milliseconds.
The Sleep function does this for you. You need to include windows.h for the sleep function to work.
It goes in miliseconds (which is 1000 btw, not 100)
Basically I divided 3000 (3 seconds) by 100 to get 30. It should count once every 30 miliseconds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <windows.h> //for sleep function to work

using namespace std;

int main()
{
   for (int i = 1; i <= 100; ++i)
   {
      cout << i << endl;
      Sleep(30);
   }

   return 0;
}
Topic archived. No new replies allowed.