Hi everyone :D
How do I make my program do something every second, like add one to a number. I have tried Sleep() and this does not work. I am using code blocks on windows with GNU GCC complier
Thanks,
Robin
Printable View
Hi everyone :D
How do I make my program do something every second, like add one to a number. I have tried Sleep() and this does not work. I am using code blocks on windows with GNU GCC complier
Thanks,
Robin
Post something you tried.
sleep();
Sleep();
I think that is it, sorry
1) Sleep() in it's various forms has a parameter.
2) where is your counter / and the output (printf, cout or whatever)
um...
All i need is an example like this:
int counter = 0;
(every second)
system("CLS");
counter = counter +1;
cout << counter;
Well unless you also output a newline, you won't see much. Most I/O is buffered.
You could do
cout << counter;
cout.flush();
So could i have an example please :)
You have all you need to make another effort yourself.
It really depends on how you want the program to act. Sleep pauses the entire thread. So if you want your program to do nothing for a full second, then sleep is the way to go. If you want to interrupt it every second and have it do something, then that is a different issue all together.
Lookup the data on sleep, in windows, it takes a measurement of milliseconds. (The windows.h sleep, there is a gnu sleep that I believe takes a measurement of seconds).
Note that sleep is not a way to keep time. If you want your periodic events to over time maintain an average time of one second, then you should query the system time to determine how long to sleep untill the next second, instead of using a hard value. The system time is not precise, but it accurate over time.
What part of Sleep() has gotten you confused?
Sleep( forSomePeriodOfTime );
You've even got a link to the manual page now, click on it and start reading.
Sorry salem,
I have found out my problem with sleep, i forgot it was a capital S and in windows.h
But this only stops the program, at least its a start :)
Sorry for any incoveniance ;)
It "pauses" the program, yes. Now all you need is some logic--an algorithm, so to speak, on how to make the program do what you want. That's the easy part.