Simple code problem

I have no idea why my code won't work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()

{	double TemperatureC, TemperatureF;
	
	cout << "Please enter the temperature in Celsius: " << endl;
	cin >> TemperatureC;
	
	int CelsiustoFahrenheit = TemperatureC * 1.8 + 32; 
	
	cout << TemperatureC "C is equivalent to " << CelsiustoFahrenheit << "F" << endl;
	cout << "Please enter the temperature in Farenheit: " << endl;
	cin >> TemperatureF;
	
	int FahrenheittoCelsius = TemperatureF % 1.8 - 32;
	
	cout << TemperatureF << "F is equivalent to " << FahrenheittoCelsius << "C" << endl;
	
	return 0;
	}
Last edited on
1
2
3
std:: cin >> TempartureC;

std:: cin >> TemperatureF;
Isn't namespace std; supposed to do that for me?
Yeah but its good programming practice using std:: .
But yeah just flip this
1
2
3
4
5
//this
<< 
//to this
>>
to get the input 
Remove the endl's after the cin's
That got rid of alot of the errors!
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{	double TemperatureC, TemperatureF;
	
	std::cout << "Please enter the temperature in Celsius: " << std::endl;
	std::cin >> TemperatureC;
	int CelsiustoFahrenheit = TemperatureC * 1.8 + 32;
	std::cout << TemperatureC << "C is equivalent to " << CelsiustoFahrenheit << "F" << std::endl;
	
	std::cout << "Please enter the temperature in Farenheit: " << std::endl;
	std::cin >> TemperatureF;
	int FahrenheittoCelsius = (TemperatureF - 32)*5/9;
	std::cout << TemperatureF << "F is equivalent to " << FahrenheittoCelsius << "C" << std::endl;
	
	return 0;
}
@OP: You should change your CelsiustoFahrenheit and FahrenheittoCelsius variable types to floats or doubles. Since you're doing floating point arithmetic, you don't want them converting back into a whole number integer.

To illustrate what I mean, google celsius to fahrenheit conversion 26C. It should come up as 78.8F.

Your program outputs 78F for both 25C and 26C.

What I recommend is you'll want variables that touch each other to have the same type, unless you're doing something specifically with a limited set of digits or data type. Such as how gas prices have those fractional cents, but they show up on your credit card as $xx.yy

Also a good tip to keep in mind is that if you do decide you want a float at the start and an int at the end for various reasons, it's better to explicitly convert them through methods such as atoi() or static_cast, instead of relying on the compiler to choose what the order of operations for conversion is.
Last edited on
Corrected
[
#include <iostream>

using namespace std;

int main()

{ double TemperatureC, TemperatureF;

cout << "Please enter the temperature in Celsius: " << endl;
cin >> TemperatureC;

double CelsiustoFahrenheit = TemperatureC * 1.8 + 32;

cout << "C is equivalent to " << CelsiustoFahrenheit << "F" << endl;
cout << "Please enter the temperature in Farenheit: " << endl;
cin >> TemperatureF;

double FahrenheittoCelsius = TemperatureF * 1.8 - 32;

cout << "F is equivalent to " << FahrenheittoCelsius << "C" << endl;

return 0;
}
]





Int only support integer . Use double instead of int.
Last edited on
Topic archived. No new replies allowed.