Time Script

I'm currently having trouble finding out a way to run simulations automatically every 3 seconds. I've been working on a small simulation console game (At them moment) and I haven't been able to find any solution. All I'm trying to manage is a way to run a loop every 3 seconds and output the results on the screen -- I have the majority of the program written already I just need to input this specific function. Help would be appreciated.
closed account (j3Rz8vqX)
Some ideas:

http://www.cplusplus.com/reference/thread/thread/
http://en.cppreference.com/w/cpp/thread/thread

Those links are self explanatory. Also, I like to invoke the use of self learning.

Hope this helps.
Last edited on
Thanks this will keep me occupied for a while.
Does this loop need to run in the background (i.e. you can do other stuff while this loop is running), or are you okay with stopping your game for 3 seconds while you wait for it to run again?

If it's the latter, you can use the clock() function in <ctime> to idly loop for 3 seconds:
1
2
3
4
5
6
7
8
while (true)
{
    cout << "This message displays every 3 seconds" << endl;
    clock_t start = clock();
    double elapsedTime = 0;
    while (elapsedTime < 3)
        elapsedTime = (clock() - start) / CLOCKS_PER_SEC;
}

http://www.cplusplus.com/reference/ctime/clock/

If you need this to run in the background, then use std::thread like Dput mentioned above. (But note that you'll need a fairly recent compiler that supports C++11 features in order to use it)
I've got something simple down at the moment and I'm hoping to build off of this and improve.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
           #include <iostream>
#include "windows.h"
using namespace std;

int Day = 1;
int main() {
	Day_Loop:
	cout<<"Day "<<Day;
	Day++;
		Sleep(2000);
	system("cls");
		goto Day_Loop;
	cin.ignore();
	return 0;

}




Very basic/choppy code -- I know.

I'd much like to see this code in a more "proper" form.

closed account (j3Rz8vqX)
Ah, I've must have misinterpret your question.

I'm trying to manage is a way to run a loop every 3 seconds


long is right, use a timer >.<

Apologies for the inconvenience.

Thought it would be running parallel to your main process.
Last edited on
I am actually looking for a way to do that too but I suppose I don't have that specific library. Thank you for leading me in that direction though.
I'll just have to get a more recent version of c++ I guess?
The C++ threading functions are only supported on C++11 so you will need a compiler that is updated to run that. A quick search will tell you if what you are using can support it.

Otherwise I recommend you look up the Boost library for threading if you want your time-based function running parallel to everything else.
closed account (j3Rz8vqX)
What platform are you using?

If windows, What IDE?

If Mingw or other IDEs that use the GCC compiler, you can verify your version in the command prompt/line using "GCC --version" without the quotes.

For GCC, you'll also need to enable C++11 features using either -std=gnu++11, -std=c++11, -std=gnu++0x, or -std=c++0x (at least one of those should work for you).

(Or, if you're using an IDE, find an option to enable C++11 features.)
Topic archived. No new replies allowed.