Hi, I wqas wondering, how exactly are timers made in C++, and how are they manipulated so that they restart after a certain amount of time...
I hope some1 out there can help...
:)
Printable View
Hi, I wqas wondering, how exactly are timers made in C++, and how are they manipulated so that they restart after a certain amount of time...
I hope some1 out there can help...
:)
the only way I really know how depends on a CPU-intensive loop and I would never recommend it to anybody, but:
then you take in length (milseconds?) from the user...Code:time_t begin,length,end;
begin=clock();
end=begin;
while(end-begin<length)
{
end=clock();
}
is that the only code thats needed, arent there any header files needed to make it work, or did u just not bother putting them in...
I didn't bother putting them in... you need <ctime>, and like I said, you need the user to put in the amount of time they want to wait... or have it passed in somehow... but here's another link to help you out if you want to go that way...
http://cppreference.com/stddate.html
there are also some non-standard methods to actually put your program's thread into a sleep (look around for sleep()), but like I said, they're non-standard. the pro to those is that they don't eat up cpu cycles...
Thank u, all i need to do now is to get use to the using the time functions, and I should be able to put it in when ever I like, cheers for the help...
:)
np... but use it sparingly, because like I said it's very CPU intensive... and as a side note, it makes for a good function...
There is more information in the Programming FAQ.
If you're using Windows, be aware that the time can be inaccruate when timing milliseconds. The results are returned with millisecond precision, but the variables are updated only (about) every 50 milliseconds. And with multitasking, the timing can be off even more.
k, ill keep that in mind when I make my programs, by the way, do u have any ideas on how to make the clock start on the user's input, not when the program starts. I tried this code below, but it didn't work, any ideas???
(The program is just an example)
Code:#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(int argc, char *argv[])
{
int num;
cout << "Please press '1' to start the clock" << endl;
cin >> num;
if(num == 1)
{
while(num = 2)
{
time_t begin,length,end;
begin=clock();
end=begin;
cout << clock() << endl;
}
}
else
{
cout << "Please press '1'" << endl;
int main();
}
system("Pause");
return 0;
}
You might want to check your program logic. Specifically, that while loop looks a little out of place.
I admit, it is a bit shabby, but it was rushed and my compiler doesn't pick a fight with it, so for now its ok... but how can I make the timer start on the user's input, and not when the program starts. Also, is there a way to get the computer to read off the clock so that when it gets to a certain value, it outputs something, because I was thinking of having it output random strings after a random number of seconds...
I a tad stuck rite now, help would be greatly valued...
:)
cout << "enter whatever" << endl;
cin >> whatever;
//get current time here---call it startTimer
//do something here
//get current time here---call it stopTimer
//subtract startTimer from stopTimer
//see if difference between the two exceeds your time limit or do whatever else you want with it.
no, you can't have the user start the clock... the clock() function by definition returns how long the program's been running - you just have to pick up the start time and monitor the end time until the difference is the amount of time you wanted to wait...
The function clock() returns a clock_t type, not time_t. This code will only both clock_t and time_t are typedef'd to the same type.
you're right, but in this case it really doesn't matter all that much... you could use an unsigned int and get the same result...
I think time_t may be defined as a struct, in which case the code would not work. Although time_t is usually nothing more than the number of seconds since 1978(or some date like that), after 2030, or so, the number of seconds will overflow. Hence when determining the number of seconds between two time_t's, use one of the special purpose functions designed for this, such as difftime