Thread: implicit casting?

  1. #1
    Registered User Fauveboy's Avatar
    Join Date
    Apr 2014
    Posts
    42

    implicit casting?

    I'm doing an exercise which needs to find a percentage of something...
    In the code below, when I initialise x with 100.0 / 8.0 it the output is correct, however. If I write x = 100 / 8 it says 10.64. I know its something to do with the values written as integers but what is the computer actually doing here?

    Many thanks
    Code:
    
    int main()
    {
        
        //double input;
        double price = 128.0;
        double x;
        
            x = 100.0 / 8.0;
    
    
            price/= x;
            
            cout << "Price with 8% discount: " <<  price << endl;
    
    
        return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    When both the operands of a division are integers, C performs integer division. Integer division simply ignores the fractional part of the result.
    Devoted my life to programming...

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Pretty sure a proper discount would be price *= (1.0 - x); 10.64 ought to be the amount you save, and not the final price.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implicit declaration
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 11-27-2007, 07:35 AM
  2. implicit type casting
    By curlious in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2004, 06:13 AM
  3. implicit conversion
    By cj56 in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2003, 11:36 AM
  4. Implicit and Explicit
    By ripper079 in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2002, 12:22 PM
  5. What does implicit declaration mean?
    By Gamma in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2002, 11:03 PM

Tags for this Thread