Hi everyone
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
Hi everyone
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
1) Sleep() in it's various forms has a parameter.
2) where is your counter / and the output (printf, cout or whatever)
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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();
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
You have all you need to make another effort yourself.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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