Hello,
I'm tring to get into using some time functions. I looked at the <ctime> overview here: http://www.cplusplus.com/reference/clibrary/ctime/ . I am having some trouble understanding this line: endwait = clock () + seconds * CLOCKS_PER_SEC ; on this page: http://www.cplusplus.com/reference/clibrary/ctime/clock.html . Are there any parentheses that could be put int there (not nessary, just for comprehension)? And why wouldn't while(clock()<(CLOCKS_PER_SEC/seconds) work? That one seems to make sense to me. I have looked at other reference material on the subject and understand that CLOCKS_PER_SEC is a macro and am aware of some of the functions and types defined in the <ctime> header. Any or alot of explanation on the subject is greatly appreciated,
CLOCKS_PER_SEC is the number of clock ticks in one second.
Since clock () returns the current tick count, endwait will be the current time after current time with seconds delay.
For your second problem, what are you trying to do? if you want to loop for a given time, the code would look like:
'CLOCKS_PER_SEC' is not a macro. It's a #defined constant.
Note: The above code doesn't work.
1 2 3 4 5 6 7 8
//'seconds' can also be a float to loop for a fractional number of seconds.
int seconds=?;
//If you want more parens, this can also be written as:
//clock_t endwait=clock()+(seconds*CLOCKS_PER_SEC);
clock_t endwait=clock()+seconds*CLOCKS_PER_SEC;
while (clock()<endwait){
//...
}
Another option is this:
1 2 3 4 5
int seconds=?;
clock_t start=clock();
while (clock()<start+seconds*CLOCKS_PER_SEC){
//...
}
BUT, unless the compiler does some optimization, you're calculating 'endwait' on every loop. Instead of relying on optimization that may or may not be performed, it's better to just do it yourself.
I thought a macro is the same as a defined constant...? and I understand the wait(seconds) functions now. Here's another question regarding time:
How would you display the current time (like the standard 'time.exe' that comes in the command prompt)?
and how would you format it correctly? different options? using tm obj vs time_t object, and using formatting functions like acstime()?