Celsius to Fahrenheit, basic C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double V,W,X,Y,Z,A,B,C,D,E;
cout << setw(50) << "Celsius to Fahrenheit Converter \n \n" << endl;// set width to 50 places
cout << setw(50) << " #1) Enter the temperature in degrees celsius \n \n";
cin >> A;
V=(((9/5)*A)+32);
cout << setw(50) << " #2) Enter the temperature in degrees celsius \n \n";
cin >> B;
W=(9/5)*B+32;
cout << setw(50) << " #3) Enter the temperature in degrees celsius \n \n";
cin >> C;
X=(9/5)*C+32;
cout << setw(50) << " #4) Enter the temperature in degrees celsius \n \n";
cin >> D;
Y=(9/5)*D+32;
cout << setw(50) << " #5) Enter the temperature in degrees celsius \n \n";
cin >> E;
Z=(9/5)*E+32;
cout << setw(25) << "CELSIUS" << setw(12.5) << "FAHRENHEIT" << endl;
cout << "\n \n";
cout << setw(25) << A << setw(12.5) << V << endl;
cout << "\n \n \n";
cout << setw(25) << B << setw(12.5) << Y << endl;
cout << "\n \n \n";
cout << setw(25) << C << setw(12.5) << X << endl;
cout << "\n \n \n";
cout << setw(25) << D << setw(12.5) << W << endl;
cout << "\n \n \n";
cout << setw(25) << E << setw(12.5) << Z << endl;
cout << "\n \n";
system ("pause");
return 0;
}
|
The problem is that the end result is always +32 of the input. The *(9/5) is not factored...I don't understand why.
9 and 5 are both integers. Therefore the compiler will do integer division.
9 / 5 = 1
Therefore 9/5*X = 1*X = X
Other solutions are:
X*9/5 (do the multiplication first)
or
9.0/5.0 * X (do floating point division)
Thanks!
Topic archived. No new replies allowed.