why are you declaring the variable as a float? the parameter to Sleep() is DWORD (defined by Microsoft as unsigned long).

03-19-2006

siavoshkc

Because the function gets seconds.

03-19-2006

major_small

I'd probably cast it just to make sure and to make it clear...

Code:

Sleep(static_cast<unsigned long>(1000*second));

03-19-2006

Ancient Dragon

Quote:

Originally Posted by siavoshkc

Because the function gets seconds.

seconds are itegers, not floats. milliseconds are also integers.

03-19-2006

MadCow257

There shouldn't be any need to cast, it will just truncate the result to the lower millisecond.

03-19-2006

siavoshkc

Quote:

seconds are itegers, not floats. milliseconds are also integers.

Who says seconds can't be float?

Code:

s_sleep(4.65f);

Will sleep the computer for about 4650 miliseconds.

Thanks for answers so far.

03-19-2006

Ancient Dragon

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

03-19-2006

7stud

Code:

Who says seconds can't be float?

s_sleep(4.65f);

'second' can be a float because you defined 'second' to be a float:

Code:

void s_sleep(float second)

However, ms did not define the parameter of Sleep() to be a float:

Code:

VOID Sleep(DWORD dwMilliseconds // sleep time in milliseconds
);

and you are using a float as an argument when you call Sleep():

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.

Quote:

Is this code OK or I should use type casting?
Sleep(1000*second); //<<HERE