Help: Cannot Output properly

I'm sure why it won't output properly. When I input '1', I'll print '132' and if I input '2', I'll not print result. I'm not sure whats the problem.

Header file
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
#include <iostream>
using namespace std;
class temperature
{	
public:
	void choice()
	{
		cout << "Please enter 1 or 2"<<endl;
		cin  >> y;
		{
		if ( y == 1)
			{
			for (c = 0; c <= 100;c++)
			f = (c * (9/5) + 32);
			cout << f;
			}
		else if (y == 2)
			{
			for (f = 32; c <= 212; f++)
			c = ((f - 32)*(5/9));
			cout << c;
			}
		}
	}
private:
	int c;
	int f;
	int y;
};


CPP file
1
2
3
4
5
6
7
8
#include <iostream>
#include "Exercise header.h"
int main()
{
	temperature mytemperature;
	mytemperature.choice();
}
Your bracing is wrong, at least for the for loops. Any block like that that is more then 1 statement long must be enclose by braces so it should look something like this:

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;
class temperature
{	
public:
	void choice()
	{
		cout << "Please enter 1 or 2"<<endl;
		cin  >> y;
		
                 if ( y == 1)
			{
			for (c = 0; c <= 100;c++)
                            {
			     f = (c * (9/5) + 32);
			     cout << f;
                            }
			}
		else if (y == 2)
			{
			for (f = 32; c <= 212; f++)
                            {
			     c = ((f - 32)*(5/9));
			     cout << c;
                             }
			}
	}
private:
	int c;
	int f;
	int y;
};


Also, you have an extra and unnecessary set of braces around the whole if/else block.

And finally, may I ask why you are using for loops in the first place? Temperature conversion is generally a one time thing, but it looks like you are simply printing out a series of temperatures. Is that what you intended?
Last edited on
Topic archived. No new replies allowed.