tags:

views:

186

answers:

5

Can someone help me understand why this gives an output of 0?

#include <iostream>                        
using namespace std;                       

int main() {                               
    float celsius;                         
    float fahrenheit;

    cout << "Enter Celsius temperature: "; 
    cin >> celsius;
    fahrenheit = (5/9) * (celsius + 32);
    cout << "Fahrenheit = " << fahrenheit << endl;

    return 0;                             
}
+12  A: 

(5/9) will by default be computed as an integer division and will be zero. Try (5.0/9)

jdv
Or `(celsius + 32) * 5 / 9`.
Gabe
@Gabe: Incorrect Formula :P
Prasoon Saurav
+4  A: 

In C++, 5/9 computes the result as an integer as both the operands are integers. You need to give an hint to the compiler that you want the result as a float/double. You can do it by explictly casting one of the operands like ((double)5)/9;

EDIT Since it is tagged C++, you can do the casting bit more elegantly using the static_cast. For example: static_cast<double>(5)/9. Although in this particular case you can directly use 5.0/9 to get the desired result, the casting will be helpful when you have variables instead of constant values such as 5.

Naveen
I don't see why this got downvoted. It is a correct answer, though it might be a [SCITE](http://meta.stackoverflow.com/questions/19478/the-many-memes-of-meta/19535#19535) candidate.
Billy ONeal
Oh don't use an ugly cast. Type a float literal directly like jdv suggests.
Jon-Eric
@Jon: Not saying this is the best answer, but I'm saying it doesn't deserve downvotes. It's not *wrong* in any way, it's just not the *best* way.
Billy ONeal
@Naveen: Because it is a C++ tagged question, I would not recommend C-style casts. Instead I would be recommending `static_cast`.
Prasoon Saurav
@Prasoon: Ah.. that's a better reason, but IMHO still doesn't deserve downvotes for that.
Billy ONeal
@Billy ONeal: I would never downvote for such reasons. :-)
Prasoon Saurav
Why would it be more elegant to cast an int literal to a double? You can use a floating point literal or double literal and it will work fine...
Alerty
@Alerty: Because someone else editing the code is a lot less likely to clean up an explicit cast, than a seemingly pointless `.0`. Of course an experienced programmer will know the `.0` isn't pointless, but we can only wish that only experienced programmers would change code.
Ben Voigt
@Ben Voigt: Experienced programmer?? This is some really basic C++ knowledge! Using comments would be better to inform what the formula does.
Alerty
+4  A: 

Fahrenheit to celsius would be (Fahrenheit - 32) * 5 / 9

Dave18
+1 Correct formula. Don't quite understand all the downvotes.
TNi
Despite the title, the program clearly is written to convert Celsius to Fahrenheit (even though it gets the formula wrong)
Larry Wang
Also your code won't work. Try it out. (Hint: what's the outcome of anything /9 going to be in C++?)
JUST MY correct OPINION
@JUST MY correct OPINION Did you try to compile and see the result? Notice that I didn't use the brackets around constants.
Dave18
A: 

Best way would be

#include <iostream>                        
using namespace std;                       

int main() {                               
    float celsius;                         
    float fahrenheit;

    cout << "Enter Celsius temperature: "; 
    cin >> celsius;
    fahrenheit = (celsius * 1.8) + 32;// removing division for the confusion
    cout << "Fahrenheit = " << fahrenheit << endl;

    return 0;                             
}

:)

Orochi
+2  A: 

In your code sample you are trying to divide an integer with another integer. This is the cause of all your trouble. Here is an article that might find interesting on that subject.

With the notion of integer division you can see right away that this is not what you want in your formula. Instead, you need to use some floating point literals.

I am a rather confused by the title of this thread and your code sample. Do you want to convert Celsius degrees to Fahrenheit or do the opposite?

I will base my code sample on your own code sample until you give more details on what you want.

Here is an example of what you can do :

#include <iostream>
//no need to use the whole std namespace... use what you need :)                        
using std::cout;
using std::cin;
using std::endl;                      

int main() 
{   
    //Variables                           
    float celsius,    //represents the temperature in Celsius degrees
          fahrenheit; //represents the converted temperature in Fahrenheit degrees

    //Ask for the temperature in Celsius degrees
    cout << "Enter Celsius temperature: "; 
    cin >> celsius;

    //Formula to convert degrees in Celsius to Fahrenheit degrees
    //Important note: floating point literals need to have the '.0'!
    fahrenheit = celsius * 9.0/5.0 + 32.0;

    //Print the converted temperature to the console
    cout << "Fahrenheit = " << fahrenheit << endl;                            
}
Alerty