output won't write decimals

Jan 29, 2016 at 12:46am
I'm writing a code for class that prompts the user if they want to convert Celsius to Fahrenheit and vice versa. But I can't seem to figure out how why my output is always a round number I've tried float, double, int. All this while only using if and else if. any bit of help is appreciated.


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
#include <iostream>
using namespace std;

int main()
{
	int answer;
	double celsius;
	double fahrenheit;
	cout << "Type 1 to convert to Celsius and Type 2 to convert to Fahrenheit" << endl << endl;
	cin >> answer;

	if (answer == 1)
	{
		cout << "Please enter temperature in Fahrenheit" << endl << endl;
		cin >> fahrenheit;
		celsius = (fahrenheit - 32) / (9.0 / 5.0);
		cout << "Temperature is " << celsius << " degree(s) celsius" << endl << endl;

		cout << "made by Jan Cepeda" << endl << endl;
	}
	else if (answer == 2)
	{
		cout << "Please enter temperature in Celcsius" << endl << endl;
		cin >> celsius;
		fahrenheit = celsius * (9.0/5.0) + 32;
		cout << "Temperature is " << fahrenheit << " degree(s) fahrenheit" << endl << endl;

		cout << "made by Jan Cepeda" << endl << endl;
	}

	return 0;
}
Jan 29, 2016 at 12:56am
Maybe it depends on your input:
Type 1 to convert to Celsius and Type 2 to convert to Fahrenheit

1
Please enter temperature in Fahrenheit

68.1
Temperature is 20.0556 degree(s) celsius

made by Jan Cepeda



Type 1 to convert to Celsius and Type 2 to convert to Fahrenheit

2
Please enter temperature in Celcsius

19.1
Temperature is 66.38 degree(s) fahrenheit

made by Jan Cepeda



Last edited on Jan 29, 2016 at 12:56am
Topic archived. No new replies allowed.