What's the matter with my code?

I'm in a cpp class in high school, and I'm using what I've learned in there so far to try and make my own program without code samples. This isn't a required thing, it's just for fun. It's a Fahrenheit to Celsius converter... Here is the code: Other than that, this is the error: http://screensnapr.com/e/ET44vX.png
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Fahrenheit to Celsius Converter
// November 5th, 2011

#include <iostream>
using namespace std;

int main()
{
	int fahr;
	int cels;

	cout << "Fahrenheit to Celsius Converter\n";
	cels = (fahr - 32) * 5 / 9;
	cout << "Enter temperature in fahrenheit: ";
	cin >> fahr;
	cout << "The temperature in Celsius is: " << cels << '\n';
	system("pause");
	return 0;
}
cels = (fahr - 32) * 5 / 9;

What value does fahr have in this line? What value did you set it to? You haven't set it yet, so why are you trying to calculate cels using it?

If you're going to use fahr to calculate something, you have to do that calculation after you've set fahr with some value.
Last edited on
add #include <cstdlib> need for system("pause")
Last edited on
just compare with your code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	int fahr;
	int cels;

	cout << "Fahrenheit to Celsius Converter\n";

	cout << "Enter temperature in fahrenheit: ";
	cin >> fahr;
	cels = (fahr - 32) * 5 / 9;
	cout << "The temperature in Celsius is: " << cels << '\n';
	system("pause");
	return 0;
}
Last edited on
Moschops, the user is supposed to enter in the temp in fahrenheit, and that is then the value of "fahr"
It'd typical to make this mistake when begining to learn to program, I myself made this same mistake.

What you did wrong here was to put the equation to convert from fahrenheit to celcius before you had the user enter in a value for Fahr.

Also I would change the fahr from int to say a float variable since not all values will be in integer format and that will account to a loss precision.

Good catch Chriscpp!

Happy Coding!
thanx rapidxone :) float is of course is the better choice.
Moschops, the user is supposed to enter in the temp in fahrenheit, and that is then the value of "fahr"


Well then, as I put it earlier and others have emphasised, make sure that the user enters that value before you try to use it.
Topic archived. No new replies allowed.