Anonymous asked in Computers & InternetProgramming & Design · 1 month ago

Why do I keep getting zero from my code?

#include <iostream>

using namespace std;

int main()


 float fahrenheit;

 float celsius;

 cout << "Enter a temperature in faherenheit."; // asks user to enter a temperature

 cin >> fahrenheit; // takes input from user

 celsius = (5/9)*(fahrenheit-32); // converts fahrenheit to celsius

 cout << celsius;


This is supposed to convert Fahrenheit to Celsius. When I run the code and input a temperature, I get 0. I don't know why. I've looked over my code multiple times already.

6 Answers

  • 4 weeks ago

    Eddie is correct, but integer division in c/c++ will return the quotient, which is 0 for 5/9.  The c/c++ compiler should not return 1 for 5/9.  The c/c++ standard for the Arithmetic Operation "/" is the quotient of two integers.

    Since fahrenheit is a float, you will most likely have to change 32 to 32.0 as well. Test a decimal value at cin.  Test 32.1 and 29.9 and see if it returns 0.

    Other languages may not return the quotient, but actually round.  Check the languages Arithmetic operations.  For example in Visual Basic: 5/9 would return 1, but to return the quotient with integer division, the "\" would be used, thus 5\9 = 0.  If the language does not support quotient division, you would use the floor function.  I mention this to point out how integers are handled, and how well one understands what the compiler is doing with integers.

  • 1 month ago

    Two right answers, up until this point, and EddieJ was first hint: consider granting a top pick. What I'd recommend is that you start having the entirety of your constants coordinate the information type being utilized in the articulation.

  • 1 month ago

    I agree with the previous answers: (5/9) is integer arithmetic, so is evaluated to zero, remainder 5. The remainder is disregarded. You need to replace it with floating point.   

    I'm answering because I want to ad some comments, and  Yahoo! Answers currently has the comment feature disabled.  

    1) There might not actually be code to do that in the executable. An optimizing compiler would see "(5/9)" as a constant. Using integer arithmetic, the compiler would evaluate it, get the result "0". Seeing the resulting expression is 

    "0*(fahrenheit-32)", the compiler might even optimize again. The result is, it creates code for "celsius = 0.0" and avoids creating code to do arithmetic. 

    2) If I coded that line, it might be "celsius = (5.0 / 9.0) * (fahrenheit - 32.0);"

    3) An old fashioned way to debug things is to add statements to show the values as the program runs. You could add something like the following:

    cout << 5/9  << " conversion constant   ";

    You could also do something like this, to quickly turn debugging on or off:

    #define DEBUG_MODE     


    #ifdef DEBUG_MODE

    cout << 5/9  << " conversion constant   ";


    Comment out the #define when you don't want debug mode. 

  • 1 month ago

    Two correct answers, so far, and EddieJ was first (hint: think about awarding a favorite).

    What I'd suggest is that you get in the habit of having all of your constants match the data type being used in the expression.  "Silent" conversions can be a source of surprise.

    Another recommendation: Use double instead of float, unless you have a specific need for float.   I'd write that program as:

    #include <iostream>

    int main()


        double fahrenheit;

        double celsius;

        std::cout << "Enter a temperature in faherenheit. ";

        std::cin >> fahrenheit;

        celsius = (fahrenheit - 32.0) * (5.0 / 9.0); // converts fahrenheit to celsius

        std::cout << celsius << std::endl;

        return 0;}

    You could shorten that conversion to:


    ..and that would work. Since fahrenheit is double, the 32 will get converted to 32.0 before subtracting.  Since (fahrenheit-32) is double, the 5 will converted to 5.0 before multiplying.  And 9 will get converted to 9.0 before dividing.  The suggested code, though, makes the intended conversions explicit and clear (to anyone who knows C++, at least.)

  • How do you think about the answers? You can sign in to vote the answer.
  • i + i
    Lv 7
    1 month ago

    Try using '0.55555556' instead of '5/9'

  • EddieJ
    Lv 7
    1 month ago

    5/9 is integer division so the result will be an integer, so the result of the division is zero (it does NOT round up).

    Try 5.0/9, or 5/9.0, or 5.0/9.0

Still have questions? Get your answers by asking now.