basic C++ conversion program help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jmf777
    New Member
    • Aug 2008
    • 19

    basic C++ conversion program help

    Hi here is the problem when my program tries to convert Fahrenheit to Celsius it gives an answer of zero regardless of what degree you put in. Any help in solving this is much appreciated.

    Code:
    #include <iostream>
    using namespace std;
    
    int main (void)
    
    {
    	double inch_feet, inch_yard, inch_cent, inch_meter, fahrenheit;
    
    	double inch_cv_feet;
    	double inch_cv_yard;
    	double inch_cv_cent;
    	double inch_cv_meter;
    	double Celsius;
    
    	cout.setf(ios::fixed);
    	cout.setf(ios::showpoint);
    	cout.precision(2);
    	
    <snipped>
    
    	cout << " Please enter temperature in Fahrenheit to convert to Celsius: ";
    	cin >> fahrenheit;
    	Celsius = (100/180) * (fahrenheit - 32);
    	cout << " Conversion from Fahrenheit to Celsius is: " << Celsius << endl;
    
    	cout << " \nThank you for using this program.\n";
    	
    	return 0;
    
    }
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    The problem is in line 41. When C++ does integer division, it returns an integer. Thus, when you do 100/180, you get 0. To fix this, declare Celsius as a double or float and make one (or both) of the constants .0 so that the compiler knows you mean a decimal value.

    Comment

    • jmf777
      New Member
      • Aug 2008
      • 19

      #3
      Hey it worked thanks for the quick reply to my problem and the explanation of what I was doing wrong it is much appreciated.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        When posting homework please remember not to post your full code only the relevent bit you are having a problem with.

        Please read the posting guidelines

        Comment

        Working...