Temperature conversion loop program

Hi,

What is wrong with my program here?

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
  // temploop.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	double celcius;
	double farhenheit;
	int temperature;
	char unit;

	cout<<"Enter tempeature: "<<endl;
	cin>>temperature;
	while (temperature<999)
	{ 
		cout<<"Enter unit: "<<endl;
		cin>>unit;
			
		if (unit=='C')
			{
			
			farhenheit=32+((9*celcius)/5);
		cout<<"Tempeature in farhenheit is: "<<farhenheit<<endl;
		}
		else
		{ 
			celcius=(5/9)*(farhenheit-32);
			cout<<"Temperature in celcius is: "<<celcius<<endl;
		}
	}

	return 0;
}

It would help if you said what help you needed.
Are you getting compile errors? Linker errors? Wrong output? Program crash? Or???

One obvious error is line 30: 5/9 is going to do integer arithmetic. The result will always be 0.
After execution, it asks me to enter temperature, enter unit and then a debug error (run time check error 3 - the variable Fahrenheit is being used without being initialized)
Well, yeah. Look at line 25. What is the value of celsius?
Line 30: What is the value of farhenheit when this line is executed?
Hint: In both cases the values are garbage because you never initialized them, which is exactly what the debug error is trying to tell you.

Line 25,30: Don't you mean to use temperature instead of celcuis or farhenheit on the right side of the calculations?

I got it. Thanx!

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
#include <iostream>
using namespace std;
int main()
{
	int temperature=0;
	char unit;
	double fahrenheit;
	double celsius;

		while (temperature<999)
	{
		cout<<"Enter temperature: ";
		cin>>temperature;
		cout<<"Enter unit: ";
		cin>>unit;
		if (unit=='F')
		{ 
			celsius=((temperature-32)*5)/9;
			cout<<"Temperature in celcius is: "<<celsius<<endl;
		}
		else
		{ 
			fahrenheit=(((temperature*9)/5)+32);
			cout<<"Temperature in farhenheit is: "<<fahrenheit<<endl;
		}
	}
	return 0;

}


Now program should stop if I enter temperature 999 or more, but it converts the value.
How should I ask program to stop conversion when temperature>999?
Last edited on
After line 13:
1
2
  if (temperature == 999)
    return 0;

Topic archived. No new replies allowed.