By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,500 Members | 1,769 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,500 IT Pros & Developers. It's quick & easy.

basic C++ conversion program help

P: 19
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.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main (void)
  5.  
  6. {
  7.     double inch_feet, inch_yard, inch_cent, inch_meter, fahrenheit;
  8.  
  9.     double inch_cv_feet;
  10.     double inch_cv_yard;
  11.     double inch_cv_cent;
  12.     double inch_cv_meter;
  13.     double Celsius;
  14.  
  15.     cout.setf(ios::fixed);
  16.     cout.setf(ios::showpoint);
  17.     cout.precision(2);
  18.  
  19. <snipped>
  20.  
  21.     cout << " Please enter temperature in Fahrenheit to convert to Celsius: ";
  22.     cin >> fahrenheit;
  23.     Celsius = (100/180) * (fahrenheit - 32);
  24.     cout << " Conversion from Fahrenheit to Celsius is: " << Celsius << endl;
  25.  
  26.     cout << " \nThank you for using this program.\n";
  27.  
  28.     return 0;
  29.  
  30. }
  31.  
Nov 7 '08 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 849
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.
Nov 7 '08 #2

P: 19
Hey it worked thanks for the quick reply to my problem and the explanation of what I was doing wrong it is much appreciated.
Nov 7 '08 #3

Banfa
Expert Mod 5K+
P: 8,916
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
Nov 7 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.