Uninitialized local variable 'treeHeight' used.

I know it's something to do with assigning the variable a value but whenever I assign it, it just throws up another error.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  //TO BE AMENDED AND COMPLETED 
#include <iostream>			//for cin >> and cout <<
#include <cassert>			//for assert
#include <iomanip>			//for endl
using namespace std;


void drawABranch(int treeHeight);

//declare constants: symbols used for the drawing
const char BLANK(' '); 																// space around the tree symbol
const char LEAF('#'); 																// tree’s foliage symbol
const char WOOD('!'); 																//tree’s trunk symbol
const char EOL('\n');																//end of line symbol
																					//declare global variables

int branchLine;																		//loop counter for each line of the foliage

int main() 																			//draw Xmas tree
{
	int treeHeight;																	//height of the tree
																		

	void getValidHeigth(int treeHeight);
	void drawBranches(int treeHeight);
	void drawTrunk(int& treeHeight);


	
	getValidHeigth(treeHeight);
	drawBranches(treeHeight);
	drawTrunk(treeHeight);
	system("pause");																//hold the screen until a key is pressed
	return(0);
}

void getValidHeigth(int treeHeight)																//get valid height for the tree
{
	do
	{
		cout << "Enter the size of the tree (4 - 20)";									//Asks the user to choose a height for the tree.
		cin >> treeHeight;
		if ((treeHeight < 4) || (treeHeight > 20))									 //If the height of the tree is outside the boundary then it will show this error message. 
			cout << "ERROR: Invalid height! ";
		
	} while ((treeHeight < 4) || (treeHeight > 20));								 //It repeats this while the entered number is outside the boundary. 
	
		
	
} 



void drawBranches(int treeHeight)																		//draw foliage
{
	
	void drawTrunk();
	cout << EOL;																			//go to next line
	for (branchLine = 1; branchLine <= (treeHeight - 2); ++branchLine)
		drawABranch(treeHeight);																			//draw one line of foliage
		drawTrunk();
}

void drawABranch(int treeHeight)																			//draw one line of foliage
{
	
	{			
		for (int i = treeHeight; i >= branchLine; i--)												//Draws a line of a branch
		{
			cout << BLANK;																		//print a space
		}
		for (int i = 1; i <= ((branchLine * 2) - 1); i++)
			cout << LEAF;																						//print a leaf symbol
		cout << EOL;																					//End of line
	}	
}


void drawTrunk(int& treeHeight) 																				//draw trunk
{	
	


	int x;
	for (x = 0; x < treeHeight; x++) {
		cout << " ";
	}
	cout << "!";
	cout << EOL; //go to next line
	for (x = 0; x < treeHeight; x++) {
		cout << " ";
	}
	cout << "!";
	cout << EOL;

}


Any help with this would be greatly appreciated.
Oh and I know about the mix of global and local variables, I'm sorting them out one at a time.
Last edited on
I suspect it's not happy that you're passing treeHeight into functions on line 30++ without having set a value.

However, that should just be a warning. The real problem with your code is that on line 57 you're trying to call a function that doesn't exist. drawTrunk takes a parameter.

While I'm here, your function getValidHeigth is useless. It receives a copy of the variable treeHeight, and then changes the copy, and when the function ends the copy is thrown away and the variable treeHeight in the main function is completely unchanged.
Last edited on
In the declaration:
void getValidHeigth(int & treeHeight);

In the definition:
1
2
void getValidHeigth(int & treeHeight)
{


How do you manage to misspell height and spell it right on the same line? ;)
Last edited on
I didn't see the drawTrunk bit, cheers.

However it isn't a warning, it's an actual error. Whenever I make treeHeight = 0 it throws up another error
If you tell us what that other error is, we can fix that one.

What compiler are you using? Using an uninitialised variable isn't an error. It's correct C++ code. It's often a logical error on the part of the coder, but a compiler shouldn't mark it as an error.
Last edited on
error LNK2019: unresolved external symbol "void __cdecl drawTrunk(int)" (?drawTrunk@@YAXH@Z) referenced in function "void __cdecl drawBranches(int)" (?drawBranches@@YAXH@Z)

Error 2 error LNK1120: 1 unresolved externals

They're the errors when I enter int treeHeight = 0
That's a linking error. You're trying to use a function that doesn't exist. At least one of your function declarations/definitions/calls doesn't match. Like the one on line 57.

Is line 61 meant to be inside a for loop? I ask because it isn't, but the layout makes it look like it's meant to be. It's good practice to use braces { } with loops and ifs and other such.
Last edited on
I think it's meant to be outside the brackets, I've added them now anyway.
Your functions need to be declared before main, like drawABranch is. Otherwise main doesn't know they're there.
Last edited on
I want them to be local variables, not global. That's why treeHeight is inside main.
I'm not talking about your variables. All your functions need what drawABranch has on line 8.
So I put all the functions above main, now it's saying redefinition of formal parameter 'treeHeight'
tallyman wrote:
Your functions need to be declared before main, like drawABranch is. Otherwise main doesn't know they're there.

They are declared in main before they are used. So, main knows they're there.


tallyman wrote:
I'm not talking about your variables. All your functions need what drawABranch has on line 8.

They have them, in main. The only caveat is that the scope of those declarations is limited by the function they're declared in. Fortunately that doesn't constitute a problem here.
Topic archived. No new replies allowed.