wrong result?

This is a discussion on wrong result? within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> #include<conio.h> using std::cout; using std::cin; using std::endl; // functions prototypes double Celsius( double ); double Fahrenheit( double ); ...

  1. #1
    o0o
    o0o is offline
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37

    Unhappy wrong result?

    Code:
    #include<iostream>
    #include<conio.h>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    // functions prototypes
    
    double Celsius( double );
    double Fahrenheit( double );
    
    int main()
    {
        // declare varibales
        
        double tempCelsius, tempFahrenheit;
        
        // read temperatures
        
        cout<< "Enter temperature in Celsius scale:";
        cin>> tempCelsius;
        cout<< "\nEnter temperature in Fahrenheit scale:";
        cin>> tempFahrenheit;
        
        // display results
        
        cout<< "\n" << tempCelsius << "c = " << Celsius( tempFahrenheit ) << "F";
        cout<< "\n" << tempFahrenheit << "F = " << Fahrenheit( tempCelsius ) << "c";
            
    getch();
    }
    
    // definitions of Celsius() & Fahrenheit()
    
    double Celsius( double valueA )
    {
        return (5/9)*(valueA-32);
    }
    
    double Fahrenheit( double valueB )
    {
        return (9/5)*valueB+32; 
    }
    This code gives the right answer for celsius temperature when converted from fahrenheit scale but centigrade scale to fahrenheit it gives only 0F as answer.

    help needed/

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,648
    >> (5/9)
    Integer division.

    gg

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    5/9 = 0
    5.0/9.0 = .5555555555555...
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    o0o
    o0o is offline
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37
    just a slight change makes it work.
    only adding double or static_cast<double> doesn't give the required results on my dev c++ compiler.
    y? is that?


    Code:
    double Celsius( double valueA )
    {
        return (( double )(5.0/9.0)) *(valueA-32);
    }
    double Fahrenheit( double valueB )
    {
        return (( double )(9.0/5.0)) *valueB+32; 
    }

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Perhaps you should choose better names
    double Celsius( double valueA )

    Now is that to Celsius or from Celsius?
    And valueA doesn't help. If it were either c or f for example, that might be more of a clue.

    Code:
    #include<iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    // functions prototypes
    double Fahrenheit_to_Celsius( double f );
    double Celsius_to_Fahrenheit( double c );
    
    int main()
    {
        for ( double centigrade = 0.0 ; centigrade <= 100.0 ; centigrade += 10.0 ) {
            double c_to_f = Celsius_to_Fahrenheit( centigrade );   // convert it
            double f_to_c = Fahrenheit_to_Celsius( c_to_f );       // and back again
            cout << centigrade << " " << c_to_f << " " << f_to_c << endl;
        }
    }
    
    // definitions of Celsius() & Fahrenheit()
    
    double Fahrenheit_to_Celsius( double f )
    {
        double result = f - 32;
        result = result * 5.0;
        result = result / 9.0;
        return result;
    }
    
    double Celsius_to_Fahrenheit( double c )
    {
        double result = c * 9.0;
        result = result / 5.0;
        result = result + 32;
        return result;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    o0o
    o0o is offline
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37
    thanku salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Something is wrong with my file reverse program
    By abh!shek in forum C Programming
    Replies: 17
    Last Post: 05-21-2008, 11:15 AM
  2. sizeof(Header) giving wrong result
    By kapil1089thekin in forum C Programming
    Replies: 4
    Last Post: 05-05-2008, 09:30 AM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  4. cos() returning wrong result?
    By rmullen3 in forum C++ Programming
    Replies: 6
    Last Post: 08-20-2002, 11:46 PM
  5. What is wrong with this program(data structures)
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-06-2002, 12:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21