if else error

Ive got an error in the select_AC function. The error I get is uninitialized local variable A,B,C,D,E,F,G and don't know what to put instead.

I want to return the A/C unit into the main

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 #include <iostream>
using namespace std;

double calc_BTUs (double volume);
char select_AC(double BTUs);
char A, B, C, D, E, F, G;

int main()
{
	double length, width, height, Cubicfoot;
	char uI;
	
	do{
		cout << "Please enter the dimensions of the cabin in feet." << endl;
		cout << "Length: ";
		cin >> length;
		cout << "Width: ";
		cin >> width;
		cout << "Height: ";
		cin >> height;
	
		Cubicfoot = length * width * height;
	
		double BTUs = calc_BTUs(Cubicfoot);
	
		cout << "Your " << Cubicfoot << " cubic foot cabin will need " << BTUs << " BTU's of cooling power." << endl;
		
		char AC = select_AC(BTUs);
		
		cout << "This may be met by installing the '" << AC << "' type A/C unit" << endl;
		
		cout << "Do you want to try again? (Y/N) ";
		cin >> uI;
	
	} while (uI == 'y' || uI == 'Y');

	system("pause");
	return 0;
}

/*
 inputs:
	volume - length * width * height
 outputs: none
 return: returns the number of BTU's needed for the Cubic feet of the Cabin
 */
 double calc_BTUs( double volume)
 {
	 return volume/ 680 *1000;
 }
 
 char select_AC(double BTUs)
 {
	 char A, B, C, D, E, F, G;
	 
	 if (BTUs >= 14000)
		 return A;
	 else if (BTUs >= 29000)
		 return B;
	 else if (BTUs >= 45000)
		 return C;
	 else if (BTUs >= 61000)
		 return D;
	 else if (BTUs >= 75000)
		 return E;
	 else if (BTUs >= 100000)
		 return F;
	 else
		 return G;
 }
 
 
If you want to return the letter A, write return 'A';
You don't need the variables.
closed account (SECMoG1T)
maybe this is what you intended

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char select_AC(double BTUs)
 {	 
	 if (BTUs >= 14000)
		 return 'A';
	 else if (BTUs >= 29000)
		 return B'';
	 else if (BTUs >= 45000)
		 return 'C';
	 else if (BTUs >= 61000)
		 return 'D';
	 else if (BTUs >= 75000)
		 return 'E';
	 else if (BTUs >= 100000)
		 return 'F';
	 else
		 return 'G';
 }
If you want to return the letter A, write return 'A';
You don't need the variables.

Peter87, could you please explain for a dummy?
In main you have declared variables of type double named length, width, height and Cubicfoot.
Similarly, in select_AC you have declared variables of type char named A, B, C, D, E, F, and G.

In order for the variables to be useful you need to assign values to them. 'A' is a char value that represents the uppercase letter A. You could assign it to the variable A and then return A.

1
2
char A = 'A'
return A;

But why do that when you can just return 'A' directly?

 
return 'A';
When you write char A;, you're saying that A is a variable that will store a character. So, you actually need to assign it a character to store. If you want to store the character 'A' for example, you would write: char A = 'A';. The name of the variable can be anything, so you could also write char anything = 'A';. In your particular case, you just want to return the character 'A' if a particular condition is met, so you can just return 'A'; without using any variables.
Topic archived. No new replies allowed.