int main()
{
int n=1;
while (n<20)
{
Sleep(500);
std::cout << "Show this every .5 sec\n";
Sleep(2000);
std::cout << "Show this every 2 sec\n";
n++;
continue;
}
system("pause");
return 0;
}
ok I want it to show these messages exactly every .5 sec and 2 sec. What it does now is waits for the 2sec message and starts the .5 sec count after 2sec message.
so .5 message should pop up exactly every .5 sec ignoring that 2 sec message. So they should work independently somehow.
Main purpose of continue; is to stop the loop at that moment and start from beginning. Also you could use break; which stops the loop at that point. so you could put if statement in a loop saying if that condition happens break; so stop the loop.
Yeah but there's no point in having continue at the end of the loop (and it's not even conditional).
You won't be able to separate them like that in this way. Sleep doesn't work that way; it stops the entire prog. The most efficient way to do this is by threading.
ohh threading, I have read about that its have couple things happening at the same time?
could you give some short example with using thread? thanks ;D
I'm afraid I have no personal experience using them in C++, and only a bit in Java. In addition there are no standard thread libraries (yet; they're part of 0x). You'll have to go get boost.
int main()
{
int n=1;
while (n<20)
{
Sleep(500);
std::cout << "Show this every .5 sec\n";
if (n % 4 == 0)
std::cout << "Show this every 2 sec\n";
n++;
continue;
}
system("pause");
return 0;
}
Here's the non-threaded answer, which is simpler imo.