C++ help, Homework assignment


  1. Posts : 483
    Windows 8 64 bit PRO
       #1

    C++ help, Homework assignment


    So i was wondering if anyone could help me out on my homework assignment
    So far were just starting this is week 4. I need assistance trying to convert fahrenheit to kelvin
    this is what i have so far

    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
    // originally was just fah and kelvin. extraa kelvins were added in order to attempt to gain correct output.
    // output remains 273 which is incorrect
    float fah, kelvin0, kelvin1, kelvin2;
    //fix and setp so it can round to ones decimal places
    cout << fixed;
    cout << setprecision(1);
    cout << "== Fahrenheit to Kelvin converter ==" << endl;
    cout <<"Enter Fahrenheit:" << endl;
    cin >> fah ;
    // calculation has been seperated into parts indiviually because the result being output is incorrect
    // the result is always 273 original formula was kelvin = (fah - 32) * (5 / 9) + 273.15
    kelvin0 = (fah - 32);
    kelvin1 = kelvin0 * (5/9);
    kelvin2 = kelvin1 + 273.15;

    cout << "Kelvin: " << kelvin2 << endl;

    }
    My comments describe my error but if you cant read them for some reason. it // calculation has been seperated into parts indiviually because the result being output is incorrect
    // the result is always 273 original formula was kelvin = (fah - 32) * (5 / 9) + 273.15

    Also do you guys know of a great forum that deals with C++/ assisting in homework C++ related?

    i think i am going to need C++ friends in order to succeed in this class.
      My Computer


  2. Posts : 3,322
    Windows 8.1 Pro x64
       #2

    Because you're working with floats, you need to define numbers with a decimal place. The error occurs because when 5/9 is calculated, it returns the result as just 0.

    So change numbers like 32, 5, 9 to 32.0, 5.0, 9.0.

    For aesthetic purposes as well, you could also change the lines of code that deal with conversion to just a single line:

    Code:
    kelvin = 273.15 + ((fah - 32.0) + (5.0/9.0));
    cout << "Kelvin: " << kelvin << endl;
    A good site to get help with for C++ is either stackoverflow or dani web, or just shoot me a PM whenever and I'll help with what I can. You'll get your head around C++ soon enough.
      My Computer


  3. Posts : 483
    Windows 8 64 bit PRO
    Thread Starter
       #3

    Thank you so much it did work :) I looked at it and I made a small adjustment

    ((fah - 32.0) + (5.0/9.0)); to ((fah - 32.0) * (5.0/9.0));
      My Computer


  4. Posts : 3,322
    Windows 8.1 Pro x64
       #4

    Good to hear :)

    Yea, you were right to make that change. I was quite tired and it was a mistake on my behalf.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 13:16.
Find Us