type casting or not?

This is a discussion on type casting or not? within the C++ Programming forums, part of the General Programming Boards category; 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(){ ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    type casting or not?

    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

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    why are you declaring the variable as a float? the parameter to Sleep() is DWORD (defined by Microsoft as unsigned long).

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    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

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by siavoshkc
    Because the function gets seconds.
    seconds are itegers, not floats. milliseconds are also integers.

  6. #6
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    There shouldn't be any need to cast, it will just truncate the result to the lower millisecond.

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    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.
    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

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.
    Is this code OK or I should use type casting?
    Sleep(1000*second); //<<HERE
    Last edited by 7stud; 03-19-2006 at 07:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What dose this type casting mean??
    By zhoufanking in forum C Programming
    Replies: 4
    Last Post: 06-11-2008, 06:09 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 05:17 AM
  5. help with simple type casting problem
    By Jeremy_S in forum C Programming
    Replies: 2
    Last Post: 02-27-2002, 11:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21