Thread: Conversion from Fahrenheit to Celsius problem

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    13

    Conversion from Fahrenheit to Celsius problem

    I'm doing a simple code which converts an entered amount of degrees Fahrenheit to Celsius. I'm running sample test data and there appears to be a converting problem.

    Code:
    //This program will convert a temperature of Fahrenheit  to Celsius
    #include <iostream>
    using namespace std;
    
    //Function Prototypes
    int Celsius(int);
    
    int main()
    {
        int F, // # of degrees Fahrenheit
            C; // # of degrees Celsius
    
        
        cout <<"This program will convert a temperature of Fahrenheit to Celsius." << endl;
        cout <<"Please enter the amount of degrees (integer) Fahrenheit." << endl;
        cin  >> F;
        C = Celsius(F);
        cout << F << " degrees Fahrenheit converted to Celsius is " << C << endl;
    
    
    
    
    system("pause");
    return 0;
    }
    //************************************************
    //Definition of function Celsius.                * 
    //This function converts Fahrenheit to Celsius.  *
    //************************************************
    int Celsius(int F)
    {
    
        return ((5 / 9) * (F - 32)); // Formula used to convert Fahrenheit to Celsius
    
    }
    My first test data was 32 and it came out correctly with a conversion of 0 degrees Celsius. When I tried one (1), it gave me an output of zero (0) as with others numbers I have entered. Can you help me identify the problem?

    Edit:
    I know some answers will come out with a decimal, but I'm only concerned with "whole" numbers.
    Last edited by -Syntax; 01-22-2008 at 01:46 PM.

  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
    5/9 is always 0
    5.0/9.0 is better.
    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 2008
    Posts
    13
    Quote Originally Posted by Salem View Post
    5/9 is always 0
    5.0/9.0 is better.
    Since you say dividing 5.0 and 9.0 is better than 5 and 9, would it be better if I change the function Celsius to double? In example, instead of int Celsius (int), it would be double Celsius (double).

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, probably.

    The thing with math with integers only is that the results can only be integers.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you're only worried about outputting whole numbers, it's ok to leave the result as an int, as long as all of the calculations are done as doubles. Changing to 5.0/9.0 accomplishes that already, so I don't think it's necessary to change the function to use doubles unless you want the output to include decimals.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    If you write (F - 32) * 5 / 9, that will give the correct result, and avoid unnecessary convention to and from double. The key is that multiplication is done first.

    The only issue that arises is that both my suggestion, and converting to and from double, will always round the result down. This can be adjusted by adding the following modifier: (int)( (F - 32) * 5 &#37;9 > 4 ).

    Alternatively, you can have the function take and return doubles, and use 32.0, 5.0, and 9.0 as constants
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  2. Microsoft -> ISO conversion problem
    By murdercityriot in forum C++ Programming
    Replies: 18
    Last Post: 08-07-2008, 09:03 AM
  3. String conversion problem
    By Mr.Bit in forum C Programming
    Replies: 9
    Last Post: 03-04-2008, 04:39 PM
  4. simple currency conversion problem
    By sweetgem in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 12:41 PM
  5. Replies: 2
    Last Post: 02-07-2002, 09:39 AM