Why is this program converting to integers

This is a discussion on Why is this program converting to integers within the C++ Programming forums, part of the General Programming Boards category; Code: // This program converts Celcius to Fahrenheit #include <iostream.h> #include <iomanip.h> void main(void) { float Celcius, Fahrenheit; cout << ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    4

    Why is this program converting to integers

    Code:
    // This program converts Celcius to Fahrenheit
    
    #include <iostream.h>
    #include <iomanip.h>
    
    void main(void)
    {
    	float Celcius, Fahrenheit;
    	cout << "Please enter a a temperature in Celcius: ";
    	cin >> Celcius;
    
    	Fahrenheit = ((9.0/5.0)*Celcius) + 32;
    
    	cout.setf(ios::fixed);
    	cout.precision(1);
    	cout << endl;
    
    	cout << "The temperature you entered is " << Fahrenheit << " degrees Fahrenheit." << endl << endl;
    
    }
    Note the Fahrenheit calculation. If I use the following, it simply doesn't work:
    Fahrenheit = ((9/5)*Celcius) + 32;

    It's taking the 9/5 and converting it to an integer value of 1. I had to use the decimal to get it to work. Is this a feature of my compiler? - Visual C++ 6.0

    Why would it do this?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    9/5 comes out to one because it sees the 9 and the 5 and goes "ok those are integers" and follows the normal integer division rules.

    So by saying 9.0/5.0 it sees the 9.0 and the 5.0 and goes "ok those are doubles" and follows the normal double division rules.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    4
    Quote Originally Posted by Thantos
    9/5 comes out to one because it sees the 9 and the 5 and goes "ok those are integers" and follows the normal integer division rules.

    So by saying 9.0/5.0 it sees the 9.0 and the 5.0 and goes "ok those are doubles" and follows the normal double division rules.

    Thanks! I figured that's what it was doing, but I just can't fathom why they would make it that way. Is this normal? I would imagine it would have promoted those to floating points since Celcius is a float as well.

    So many things to learn and understand.......

    Thanks again for helping me out on this one.....

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    It is normal. Most compilers will actually do the division during compile time and store the result and during run time multiple it by Celcius.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting a program from unix to windows
    By waltr in forum C Programming
    Replies: 12
    Last Post: 05-24-2006, 01:51 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Converting Integers to Binary
    By cprog in forum C Programming
    Replies: 19
    Last Post: 10-03-2002, 09:20 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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