time loop

Can somebody help me how to make some function to do every 10 seconds or so?? I tried with if (clock() > 10) but after ten seconds it always call it. I know that and I tryed Sleep() but it didn t work as I thought it would so any suggestions on this matter??? thanx.
clock() keeps counting up, it doesn't just magically reset when you want it to.
Sleep() is Windows only, and it also takes a value in milliseconds (so 1 second is 1000 milliseconds)
Last edited on
well I knew those two things already but my question was how to make it work so it will do it every 10 seconds...but thx anyway..
Last edited on
1
2
3
4
5
if(clock() > next)
{
    next += 10;
    //...
}
Use std::this_thread::sleep_for()

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <chrono>
#include <thread>
#include <iostream>
 
int main()
{
    std::chrono::seconds interval( 10 ) ; // 10 seconds
    for( int i = 0 ; i < 10 ; ++i )
    {
       std::cout << "tick!\n" << std::flush ;
       std::this_thread::sleep_for( interval ) ;
    }
}
well not like that.. It doesn t work.. I ll try to explain one more time.. for example you have
while loop with switch statement an you set energy on for example 100 and what is my probelm is that it will be still less and less in i don t know maybe 30 seconds... i ll post you part of a code here.. maybe it will be better to understand what I want.. sorry for my english btw. i am not native english speaker.

=>


while (fEnd)
{
rex.growUp();
if (sec >= death)
{
cout << "I am really sorry but " << name << " died. He was too old. Would you like another pet?";
}
cout<< name <<" is " <<rex.getAge()<<" years old and "<< rex.getHappines()<< "% happy and "<< rex.getHunger()<<"% full and has "<<rex.getEnergy()<<"% energy."<<endl ;
int choice;
cout <<"(1) Feed it (2) Pet it (3) Let it sleep (4) Play with it (5) Load or restart game (0) End\nChoose number: ";
cin >> choice;
switch (choice)
{
case 0:
char progres;
cout << "Do you want to save the progres?? y/n\n";
cin>> progres;
if (progres == 'y')
{
fEnd = false;
break;
}
else
{
cout << "Thank you for playing. Please press enter to quit.";
cin.ignore();
cin.get();
return 0;
}
case 1:
system("cls");
back3:
if (rex.getHunger()<=100)
cout <<rex.getHunger()<<"% full"<<endl;
if (rex.getHunger()>100)
{
rex.setHunger(100);
cout <<rex.getHunger()<<"% full"<<endl;
}
.............. and so on........

thank you...
thx JLBorgers i didn t see yours before well but will it work even if I do something inside of while loop?
You may want to have a look at the following. With this, you can run other code while 'sleeping'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//Timer.

#include <Windows.h>
#include <iostream>
#include <conio.h>

void updateThingsHere()
{
	std::cout << "You just updated energy.\n";
	std::cout << "You just updated health.\n\n";
}

int main()
{
	double startTime = GetTickCount();

	while( true )
	{
		double currentTime = GetTickCount() - startTime;

		if( currentTime >= 1500 ) //1 and a half seconds.
		{
			updateThingsHere();

			//Reset the timer.
			startTime = GetTickCount();
		}

		//Run other code here while not updating.

		char key = ' ';

		if( _kbhit() )
			key = _getch();

		if( key == 13 ) //The ENTER key.
			break; //Quit the while loop
		
		else if( key != ' ' )
		{
			std::cout << "You pressed: " << key << '\n';       
			key = ' ';
		}
	}


	return 0;
}


This will run the update function every 1 and a half seconds, but still run the other code while it's waiting for the current time to go greater/equal to 1500 milliseconds.
Last edited on
> will it work even if I do something inside of while loop?

Do you mean do something at the same time that you are waiting? No, the above code will not work; std::this_thread::sleep_for() would block for the duration of the sleep. Is that what you want to do?

A second question. The 'every 10 seconds' that we are talking about - is that elapsed time (as in 4:52:35 PM to 4:52:45 PM ) or processor time (10 seconds of CPU time used in the interim)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <chrono>
#include <iostream>

struct timer
{
    typedef std::chrono::steady_clock clock ;
    typedef std::chrono::seconds seconds ;

    void reset() { start = clock::now() ; }

    unsigned long long seconds_elapsed() const
    { return std::chrono::duration_cast<seconds>( clock::now() - start ).count() ; }

    private: clock::time_point start = clock::now() ;
};

// function to be called every ten seconds
void foo() { static int n = 0 ; std::cout << "\n*** " << ++n << ". foo ***\n\n" ; }

int main()
{
    timer t ;
    foo() ;

    for( int i=0 ; i < 10 ; ++i )
    {
        // do something
        std::cout << "doing something...\n\tenter a char: " ;
        char c ; std::cin >> c ;
        std::cout << '\t' << c << '\n' ;

        // if 10 seconds have elapsed, call foo again
        if( t.seconds_elapsed() > 9 ) { foo() ; t.reset() ; }
    }
}
well i want elapsed time while playing game it should count to 10 or 20 or so and after this time it should change energy by itself for example every 10 sec aka 10 000 milisec energy go down by 10 percent..

and i have never worked with chrono library so I probably should look this up for my project..

thx anyway..
I, myself, haven't used chrono before either.

But, if you take a look at my post above, all you have to include in #include <windows.h> which you already have included, as you're using system();.

Then it's just math to create/reset the loop.

If you copy and paste my code, to try it. You'll see that it will cout "you have just updated...".

This is where you would do your calculations for changing you health/energy variables etc.

And while it's not updating these, if you press a button, it will tell you what you have pressed. i.e. Running code while waiting to update!
it works perfectly fine thanx Lynx876.. you helped me a lot.. I was stuck here...
No problem. Glad to help (:
Topic archived. No new replies allowed.