Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   C++ (https://www.askmehelpdesk.com/forumdisplay.php?f=439)
-   -   No error messages, but output keep (https://www.askmehelpdesk.com/showthread.php?t=144155)

  • Oct 23, 2007, 05:52 PM
    Rebekah_S
    No error messages, but output keep
    I am very new to the world of C++, but was asked to program a Fahrenheit to Celsius converter application. I FINALLY got the code to not give me any error messages, but every time I execute the application, the output answer for celsius is 0. Could anyone help me determine where the problem is??




    #include <iostream>
    #include <iomanip>

    using std::cout;
    using std::cin;
    using std::endl;
    using std::setprecision;
    using std::ios;
    using std::setiosflags;


    //function prototypes
    int getFahrenheit();
    int calcCelsius(int, int);


    int main()
    {
    int far = 0;
    int subt = 32;
    int cel = 0;

    far = getFahrenheit();





    //display celTemp
    cout << setiosflags(ios::fixed) << setprecision(0);
    cout << "Celsius temperature is: " << cel << endl;

    return 0;
    } //end of main function

    //*****function definitions*****
    int getFahrenheit()
    {
    double FahrenH = 0.0;
    cout << "Enter temperature in Fahrenheit: ";
    cin >> FahrenH;
    return FahrenH;
    } //end of getFahrenheit function

    int calcCelsius(int far, int subt)
    {
    int cel = 0.0;
    cel = (5.0*(far - subt)/9.0);
    return cel;
    } //end of calCelsius function
  • Oct 23, 2007, 06:18 PM
    Marriedguy
    I think you are using the wrong data type "int" this for whole numbers. "float" is used for decimal numnbers. Hope this helps.
  • Oct 23, 2007, 06:30 PM
    Rebekah_S
    changing to "float" didn't seem to help... my solution keeps being "0" when executed. It seems to be more of a logical-type problem. (i.e. is part of the equation being calculated as 0?)
  • Oct 24, 2007, 05:53 AM
    asterisk_man
    You never called the calcCelsius function to set the cel variable in main(). With your current implementation you need to add the following code below where you have far = getFahrenheit():
    cel=calcCelsius(far, 32)

    On a side note, why is subt a parameter of calcCelsius()? Is there any case where this isn't going to be 32?

  • All times are GMT -7. The time now is 11:07 PM.