Djanvk said:
Ok I'm working on a program where you have say x number of seconds to
do something. I'm at a loss on how to go about running a timer
function. Can anyone point me in the right direction or post a
snippet of code for me to look at?
The program I'm trying to write is a 2nd grade math program to help my
daughter with her addition and subtraction.
A timer is a service provided by the operating system for client programs to
use. *Your* compiler may list a function named something such as "sleep()".
But for what you want to do a simpler way is to just burn up some time.
[sleep() would take control back from your program and give the time to
someone else to use until the timer expired.]
Try running this program, experimenting with the input parameter, try an
initial value such as 5,000. Or, you can be scientific if you wish by using
the clocks/sec output provided.
Formally, the type returned by a call on clock() is clock_t, rather than an
int. But I didn't declare it as such, it gives things an esoteric look I
would rather avoid in situations such as this..
#include <iostream>
#include <ctime>
using namespace std;
// burn up n clock cycles
void delay(int n)
{
int now = clock();
int then = now + n;
while(now < then)
// do nothing
now = clock();
}
//===============
int main()
{
cout << "This system advances clock " << CLOCKS_PER_SEC << " times per
second.\n";
int cycles;
cin >> cycles;
while(1)
{
cout << "Start\n";
delay(cycles);
cout << "Fin\n";
}
}