2

I am a student of software development and I need to convert from Fahrenheit to Celsius but my code calculates it wrong. Here is my code:

int main() {
    // configure the out put to display money
    cout.setf(ios::fixed);     //no scientific notation
    cout.setf(ios::showpoint); //show decimal point
    cout.precision(0);         //two decimal for cents

    int fahrenheit = 0 ;
    int celsius = 5/9*(fahrenheit-32);

    cout << "Please enter Fahrenheit degrees:  ";
    cin >> fahrenheit ;

    cout << "Celsius:  " <<  celsius << endl;

   return 0;
}
Andreas
  • 2,455
  • 10
  • 21
  • 24
  • `5/9` is zero. Oops. And what's your objection to scientific notation? It's really rather neat you know. – Bathsheba Jan 21 '19 at 07:58

3 Answers3

2

your formula is using int : 5/9 meaning that you are loosing some precision change the 5 to 5.0 or if you want change Celsius to float

moshe
  • 29
  • 1
2

There are four mistakes in your code.

1) The main point is to realise that the computer does things in the order you ask it to. Obviously the correct order is a) ask the user to enter a temperature b) convert it to celsius. But your code does it the other way round. Here's your code with some comments of mine

// convert fahrenheit to celcius
int celsius = 5/9*(fahrenheit-32);

// ask user to enter fahrenheit temperature
cout << "Please enter Fahrenheit degrees:  ";
cin >> fahrenheit ;

Hopefully it's now obvious that you have things the wrong way round

2) Second error is that you have choen the wrong type for your variables. Temperature is not an integral quantity (nothing wrong with saying the temperature is 80.5 degrees for instance). So you should choose a floating point type for your variables, float is one possibility.

3) Third error is quite technical but important to understand. In your equation you wrote 5/9, both 5 and 9 are integers, so the computer is going to perform integer division, which means whatever the mathematical result of the division the computer is going to drop the fractional part of the result, leaving an integer. So mathematically 5/9 is 0.555555..., droping the fractional part leaves 0, so your equation is the same as 0*(fahrenheit-32) which is obviously not going to give the right result. Use 5.0/9.0 instead of 5/9 that way you get floating point division.

4) Final error is quite trivial

cout.precision(0);         //two decimal for cents

If you want two decimal places it should be

cout.precision(2);

Finally, its not an error, but the comments about money are inappropriate in a program about temperature.

Here's a version of your code with these errors fixed

int main() {
    cout.setf(ios::fixed);     //no scientific notation
    cout.setf(ios::showpoint); //show decimal point
    cout.precision(2);         //two decimal places


    float fahrenheit;
    cout << "Please enter Fahrenheit degrees:  ";
    cin >> fahrenheit;

    float celsius = 5.0/9.0*(fahrenheit-32.0);
    cout << "Celsius:  " <<  celsius << endl;

   return 0;
}

I'm sure you're surprised that a short program can have so many mistakes. It just emphasises that you have to be careful and precise when you write code.

john
  • 85,011
  • 4
  • 57
  • 81
1

If you have to use int, then you should perform division as last step to reduce loss of precision for int-types. But keep in mind, that this may cause int-overflows (shouldn't be a problem for temperatures...)

#include <iostream>
using namespace std;
int main() {
    // configure the out put to display money
    cout.setf(ios::fixed);     //no scientific notation
    cout.setf(ios::showpoint); //show decimal point
    cout.precision(0);         //two decimal for cents

    int fahrenheit = 0 ;

    cout << "Please enter Fahrenheit degrees:  ";
    cin >> fahrenheit ;
    int celsius = 5*(fahrenheit-32)/9;
    cout << "Celsius:  " <<  celsius << endl;

   return 0;
}
meddle0106
  • 1,292
  • 1
  • 11
  • 22
  • 1
    It's pretty clear that using integers is a mistake by the OP (otherwise why set fixed and showpoint?). – john Jan 21 '19 at 07:17
  • A 16 bit `int` will overflow if you're interested in converting the temperature of the centre of the sun from C to F. – Bathsheba Jan 21 '19 at 08:00