Is this code OK or I should use type casting?
Code:#include<windows.h> void s_sleep(float second)( Sleep(1000*second); //<<HERE } int main(){ s_sleep(3); //Sleep 3 seconds return 0; }
Is this code OK or I should use type casting?
Code:#include<windows.h> void s_sleep(float second)( Sleep(1000*second); //<<HERE } int main(){ s_sleep(3); //Sleep 3 seconds return 0; }
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
why are you declaring the variable as a float? the parameter to Sleep() is DWORD (defined by Microsoft as unsigned long).
Because the function gets seconds.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
I'd probably cast it just to make sure and to make it clear...
Code:Sleep(static_cast<unsigned long>(1000*second));
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
seconds are itegers, not floats. milliseconds are also integers.Originally Posted by siavoshkc
There shouldn't be any need to cast, it will just truncate the result to the lower millisecond.
Who says seconds can't be float?seconds are itegers, not floats. milliseconds are also integers.
Will sleep the computer for about 4650 miliseconds.Code:s_sleep(4.65f);
Thanks for answers so far.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
You can write your function to accept anything you want, but Sleep() takes integers. not floats.
<<Will sleep the computer for about 4650 miliseconds
yes, but that is an integer
'second' can be a float because you defined 'second' to be a float:Code:Who says seconds can't be float? s_sleep(4.65f);
However, ms did not define the parameter of Sleep() to be a float:Code:void s_sleep(float second)
and you are using a float as an argument when you call Sleep():Code:VOID Sleep( DWORD dwMilliseconds // sleep time in milliseconds );
Sleep(1000*second)
An automatic conversion from a float to a DWORD may take place, and the automatic conversion may confuse someone reading your code. If you don't want to rely on an implicit conversion, and you want to make it clear what is actually happening, you can make an explicit cast.
Hey, you asked the question.
Is this code OK or I should use type casting?
Sleep(1000*second); //<<HERE
Last edited by 7stud; 03-19-2006 at 08:34 PM.