//TO BE AMENDED AND COMPLETED
#include <iostream> //for cin >> and cout <<
#include <cassert> //for assert
#include <iomanip> //for endl
usingnamespace std;
void drawABranch(int treeHeight);
//declare constants: symbols used for the drawing
constchar BLANK(' '); // space around the tree symbol
constchar LEAF('#'); // tree’s foliage symbol
constchar WOOD('!'); //tree’s trunk symbol
constchar 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.
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.
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.
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.
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.