Creating a timer.

I'm a novice programmer in C++ and I'm wondering how to update a value of an integer periodically. For example, increasing the value of an integer by 10 every 5 seconds. Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <ctime>

int main () 
{
	// http://www.cplusplus.com/reference/ctime/
	time_t start = time (NULL); 
	int val = 0;

	while(true) // entire code must be inside
		    // this loop. 
	{
		time_t update = time(NULL); // check time now
		if (update - start > 10) // check if it was more
					 // than 10 seconds since
					 // last check
		{
			start = update;
			std::cout << (val += 5);
		}
	}
}
Last edited on
Topic archived. No new replies allowed.