program converting Celsius to Fahrenheit

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zelmila19
    New Member
    • Apr 2008
    • 16

    program converting Celsius to Fahrenheit

    Hi
    I was doing this program on Visual Studio 2005 where the program converts C to F and vise versa but after debugging it, it doesnt give the expected result. Could someone tell me what is wrong with my code???


    #include <iostream>
    #include <iomanip>

    using namespace::std;
    using std::fixed;

    int main()
    {
    int conversionType = 0;
    double temp = 0.0;
    double result = 0.0;

    cout << "Enter -1 (F to C) or 2 (C to F): ";
    cin >> conversionType;
    cout << "Enter temperature: ";
    cin >> temp;

    if (conversionType == '-1')
    {
    result = (temp - 32) * 5 / 9 ;
    }
    else if (conversionType == '2')
    {
    result = temp * 9/5 + 32;

    } //end if

    cout << "Result: " << result << endl;

    return 0;
    } //end of main function
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Note that in C++ (and most programming languages), integer division is different than floating point division. Only integers are returned, so 9/5 is 1, and 5/9 is 0. You should replace those with 9.0/5.0 and 5.0/9.0, respectively.

    Comment

    • vhm
      New Member
      • Apr 2008
      • 4

      #3
      Originally posted by zelmila19
      Hi
      I was doing this program on Visual Studio 2005 where the program converts C to F and vise versa but after debugging it, it doesnt give the expected result. Could someone tell me what is wrong with my code???


      #include <iostream>
      #include <iomanip>

      using namespace::std;
      using std::fixed;

      int main()
      {
      int conversionType = 0;
      double temp = 0.0;
      double result = 0.0;

      cout << "Enter -1 (F to C) or 2 (C to F): ";
      cin >> conversionType;
      cout << "Enter temperature: ";
      cin >> temp;

      if (conversionType == '-1')
      {
      result = ((temp - 32) * 5) / 9 ;
      }
      else if (conversionType == '2')
      {
      result = ((temp * 9) /5) + 32;

      } //end if

      cout << "Result: " << result << endl;

      return 0;
      } //end of main function


      Thats right Gagnon. Here you have something a litle more polished but very faithfull to the original.


      <Full code solution removed>
      Last edited by Banfa; Apr 14 '08, 09:12 AM. Reason: Full Code solution removed

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        vhm, please note we do not allow the posting of full code solutions on this site for a number of reasons.

        I suggest you read our Posting Guidelines

        Banfa
        Administrator

        Comment

        • SpecialKay
          New Member
          • Mar 2008
          • 109

          #5
          If i were you, i would use
          C = F - 32 / 1.8
          and
          F = C + 32 * 1.8

          solves all the problems.

          Comment

          • phiefer3
            New Member
            • Jun 2007
            • 67

            #6
            Originally posted by SpecialKay
            If i were you, i would use
            C = F - 32 / 1.8
            and
            F = C + 32 * 1.8

            solves all the problems.
            Except that those are actually wrong. They should be:

            C = (F - 32) / 1.8
            The parentheses are important, other wise it divides 32 by 1.8 and subtracts that from F, and the second one should be

            F = C * 1.8 + 32

            Comment

            Working...