CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2009
    Posts
    2

    Simple Assignment Help

    New guy here. I'm barely in my second C++ class, and we are doing some review and I cannot figure out why this won't give me what I want. The problem is to create a function to convert Fahrenheit to Celsius. It's to be enclosed in a loop so that my output is a list that shows what 1-20 degrees fahrenheit is in Celcius. This is my code:


    // Celcius Temperature Table

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

    double celsius(double);

    int main()
    {
    double fah, cel;
    cout << fixed << showpoint << setprecision(1);

    cout << "Fahrenheit\tCelcius\n";
    cout << "----------------------\n";
    for (fah = 0; fah <= 20; fah++)
    {
    cel = celsius(fah);
    cout << fah << "\t" << cel << endl;
    }
    return 0;
    }

    double celsius(double fah)
    {
    return ((5 / 9) * (fah - 32));
    }



    I can't see what is wrong, but whenever I compile it, it loops like it should and lists Fahrenheit 1-20, but in the celcius column every value is 0.0. Can someone help me out, or at least give me a hint about what I'm doing wrong?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Simple Assignment Help

    Hint: In C(++), 5/9 is not the same as 5.0/9.0.

  3. #3
    Join Date
    Mar 2009
    Posts
    2

    Re: Simple Assignment Help

    Oh man. That was it. I've been going crazy trying to find out what was wrong. I definitely won't forget this again. Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured