Thread: Problem

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Problem

    Why does 900/1000=0?
    Example:
    Code:
    if(900/1000==0){
    MessageBox(hwnd,"Huge problem here","sniff",MB_OK);
    }
    Or:
    Code:
    if(800/1000==0){
    MessageBox(hwnd,"Huge problem here","sniff",MB_OK);
    }
    Or:
    Code:
    if(700/1000==0){
    MessageBox(hwnd,"Huge problem here","sniff",MB_OK);
    }
    Last edited by maxorator; 10-14-2005 at 12:10 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Because it's done using integer arithmetic.

    900/1000 is 0
    900.0 / 1000.0 is 0.9
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Because those are integers.
    Why drink and drive when you can smoke and fly?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Always remember
    Code:
    int/int=int
    That is there will not be any decimal part!!!

    Solution is to convert one divisor or divident into float.You can do it as explained by Salem or by typecasting
    Code:
    if(900/(float)1000==0)
    or
    if(900/float(1000)==0)
    By typecasting you force the compiler to convert 1000 into float

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Not to rag on you or anything, but might I suggest you spend some time learning the basics before you go and attempt to create message boxes and other GUI.
    Sent from my iPad®

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Well, I got the point Salem's post, and I already got it working. Thanks anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM