Dividing two integers resulting in a decimal.

Discussion in 'C++' started by SamJacob, Sep 12, 2010.

  1. SamJacob

    SamJacob New Member

    Joined:
    Sep 12, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    result = number1 / number2;
    I am new to C++, but I have some knowledge in Java.
    What my program does is, the program asks the user two numbers. The user could only select, one, two, or zero. Then the user would select an arithmetic operator: plus, minus, times, divide. I figured everything out except the division part.
    When the user select one divide by two, the answer should come out as 0.5, but it comes out as 0. All three variables are int values, so I would need to convert them into a float or a double. But how would I cast them? I've tried doing this, float(number1), and many more similar to that, but it wouldn't work.

    Thanks,
    Sam
     
  2. go4expert

    go4expert Moderator

    Joined:
    Aug 3, 2004
    Messages:
    306
    Likes Received:
    9
    Trophy Points:
    0
    Use
    Code:
    result = ((double))((double)number1 / (double)number2);
     
  3. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    Itz a wrong procedure...Dnt have to use so many casting...only one casting will do...like the below:-
    Code:
    ((double)number1/number2)
    
    Now, let me tell the reason...the implicit casting follows the cascading rule from int to float.. i.e. if one number is float and other is int then the int part is converted to float first and then the result is evaluated...so many casting are not needed...I just need to convert any of the number to double thatz all...On the other hand if you are storing the result to "result" variable then the variable should be double....
    Thank you!!
     
  4. go4expert

    go4expert Moderator

    Joined:
    Aug 3, 2004
    Messages:
    306
    Likes Received:
    9
    Trophy Points:
    0
    I agree that my method does extra casting but I don't thing it is wrong. Your syntax also does the same thing without actually specifying it i.e. converts each number into the double.
     
  5. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    I didn't say itz wrong..i said itz a wrong procedure...If you keep on increasing the code complexity like this then I dnt think that itz should be said as a good procedure...if you give such solutions to a newbie then the person would get confused...u should have given him the explanations why u did like that...u placed double wherever u found the space for it.....
     
  6. go4expert

    go4expert Moderator

    Joined:
    Aug 3, 2004
    Messages:
    306
    Likes Received:
    9
    Trophy Points:
    0
    Ohh I thought you meant it is wrong. My mistake.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice