Using time?

So I'm trying to learn how to use time for time based movement and what not. I did this only knowing the command time(&variable)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
time_t timer;
int I, X;

void main()
{
	time(&timer);
	X=timer;
	while(I==5)
	{
		time(&timer);
		if(X+5==timer)
		{
			I++;
			cout<<"YAY"<<endl;
		}
	}
	getch();
}

Theres probably some other better way to do it... Obviously but for now I see it as putting current time in X.
start while
take in time constantly until I is 5
constantly asking is time>X(preset time 5 seconds ahead)
if it is
display message and add one to I

Why doesnt this display my message after 5 seconds?
a) nobody said that time_t contains seconds.
b) nobody said program will not skip some time point where if(X+5==timer) (Do not use === when dealing with time)

In your case you can do this:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <chrono>
#include <thread>
 
int main()
{
    std::cout << "Hello waiter" << std::endl;
    std::chrono::seconds duration( 5 );
    std::this_thread::sleep_for( duration );
    std::cout << "Waited 5 seconds\n";
}
Topic archived. No new replies allowed.