Table comparing celcius and fahrenheit. Keep getting error

I'm trying to write a program that shows a table of the Celsius temperatures from 0 to 10, and the Fahrenheit equivalents right below. I used F = 9/5 * celcius + 32 as the formula to convert Celsius to Fahrenheit. When I run, it says "variable 'celcius' is not initialized." What am I doing wrong?

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

int main()
{
	int celcius;                
    

	cout << "Celcius: ";

	
	for (int c = 0; c <= 10; c++)  
	{
		cout << c << " ";
	}

	int fah;  
	fah = (9 / 5) * celcius + 32;
	cout << "\nFahrenheit: ";
	cout << fah << endl;

	

	return 0;
}
What am I doing wrong?

Exactly what it says. You are not initializing or assigning to the variable celcius before using it as if it has a meaningful value.

What value do you think celcius has on line 18?

(And btw, 9/5 is integer division and has the value 0. Use 9.0/5.0 and change the type of celcius and fah to double.)
Thanks, I've changed it to 9.0/5.0 and used the double type. As for initializing, the problem is that I don't know how to do that. My teacher didn't explain it very well.

As for the value of celcius, I don't want to assign it a single number. Instead, I want to assign it all of the numbers from 0 to 10, which will then be placed into the formula and output the fahrenheit equivalents. So how do I get the celcius table to output its fahrenheit equivalents?
As for the value of celcius, I don't want to assign it a single number. Instead, I want to assign it all of the numbers from 0 to 10, which will then be placed into the formula and output the fahrenheit equivalents. So how do I get the celcius table to output its fahrenheit equivalents?


The variable celcius can only hold a single number at a time, so you [b]do[/tt] want to assign it a single number. You just want to do so more than one time. And doing things more than one time kind of begs for a loop. It seems you're familiar, you just haven't taken the code to it's logical conclusion.

After the first loop displays the degrees in celcius, you need a second loop to display the degrees in fharenheit. You can use the loop counter to represent the degrees in celcius as you did in the first loop. Just convert it to fahrenheit inside the loop body, then output the result (also inside the loop body.)
What should celcius be initialized as? This is what I've got so far:

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

int main()
{
	double celcius;


	cout << "Celcius: ";


	for (double c = 0; c <= 10; c++)
	{
		cout << c << " ";
	}

	double fah;
	cout << "\nFahrenheit: ";
	

	for (double f = 0; f <= 10; f++)
	{
		cout << f << " ";
		fah = (9.0 / 5.0) + 32;
		cout << fah << endl;
	}
	



	return 0;
}
In your second loop, instead of using 'f' in the loop header, use the variable 'celsius' like so:

for (double celcius = 0; celcius<= 10; celcius++)

Then you can use 'celcius' in the loop body (in the formula) to calculate 'fah' and then print 'fah'.

Thanks, the code is working now. Is there anything I else I should change or add to make the code look more clean, or is this good enough?
Can you post your full code? I am almost 100% sure it can be improved, at least stylistically.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{

	cout << "Celcius: "; cout << "Fahrenheit: \n";
	
	double fah;
	
	

	for (double celcius = 0; celcius <= 10; celcius++)
	{
		fah = (9.0 / 5.0) * celcius + 32;
		cout << celcius << " ";
		
		cout << fah << endl;
	}
	return 0;
}
Arslan7041 wrote:
In your second loop, instead of using 'f' in the loop header, use the variable 'celsius' like so


One shouldn't use the double type in the condition of a loop. They aren't exact and doing so could lead to off by one errors.

Celsius could still be int, and used in the for loop, but expression on line 15 becomes double.

If Celsius was to remain a double, then it is preferable to work how many times one needs to loop as an int, then use that in the loop condition. Should be easy to work out a little maths for that :+)

@mike0910
Btw fah isn't a good variable name, use something meaningful. I think the idea of having to abbreviate everything to death is a false concept.

Good Luck.
@TheIdeasMan: Good catch.

@Mike: you might also want to make your output look better by using setw() and other iomanip tools.
Topic archived. No new replies allowed.