Temperature conversion

Discussion in 'C++' started by deanfamily11, Sep 10, 2005.

  1. deanfamily11

    deanfamily11 Guest

    I'm trying to have this program do a simple temperature conversion from
    Fahrenheit to Celsius. I have confirmed that the other variable is
    receiving and calculating the conversion, but it is just outputting as "0".
    Any thoughts? (Code is below)


    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main()
    {

    //declare variable
    int fahre;
    double centi;

    //get the temperature
    cout << "Welcome to the temperature converter." << endl << endl;
    cout << "Enter a temperature in degrees Fahrenheit: ";
    cin >> fahre;
    cout << endl;
    cout << endl;
    cout << endl;

    //convert the temperature to centigrade
    centi = (fahre - 32) * (5 / 9);

    //output both the original temperature and the converted one
    cout << "Below is your original and converted temperatures." << endl;
    cout << "Degrees F: " << fahre << endl;
    cout << "Degrees C: " << centi << endl;

    return 0;
    }
    deanfamily11, Sep 10, 2005
    #1
    1. Advertising

  2. deanfamily11

    Shezan Baig Guest

    deanfamily11 wrote:
    > I'm trying to have this program do a simple temperature conversion from
    > Fahrenheit to Celsius. I have confirmed that the other variable is
    > receiving and calculating the conversion, but it is just outputting as "0".
    > Any thoughts? (Code is below)
    >
    > [snip]
    >
    > //convert the temperature to centigrade
    > centi = (fahre - 32) * (5 / 9);



    Change "(5 / 9)" to "(5.0 / 9.0)". When you do 5/9, it uses integer
    arithmetic, so the result becomes 0. So you are essentially doing
    "(fahre - 32) * 0"

    Hope this helps,
    -shez-
    Shezan Baig, Sep 10, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    1,642
    Joe Knapka
    Jul 24, 2006
  2. Flemming Hansen

    CPU temperature reading with C

    Flemming Hansen, Mar 16, 2005, in forum: C Programming
    Replies:
    8
    Views:
    1,235
    Thomas Matthews
    Mar 17, 2005
  3. Newbie

    CPU temperature reading

    Newbie, Dec 9, 2005, in forum: C++
    Replies:
    1
    Views:
    352
    Victor Bazarov
    Dec 9, 2005
  4. arnuld
    Replies:
    4
    Views:
    464
    arnuld
    Mar 4, 2007
  5. Replies:
    21
    Views:
    1,629
Loading...

Share This Page