Creating a christmas tree

Im new to programming and im struggling with creating a christmas tree. the code im cureently using doesnt seem to be right and ive got rather confused with why it isnt.

the current code i am using compiles and shows the following:

#
#
#
#
#
#

depending on the size of tree i want.

however i want the code to produce this:


#
###
#####
#######

please help.

#include <iostream> //for cin >> and cout <<
#include <cassert> //for assert
#include <iomanip>
#include <ctime>// fpr date

using namespace std;

//declare constants: symbols used for the drawing
void getValidHeight();
void drawBranches();
void drawTrunk();
void drawABranch();

const char BLANK( ' ');
const char LEAF( '#');
const char WOOD( '|');
const char EOL( '\n'); //end of line symbol
//declare global variables
int treeHeight; //height of the tree
int branchLine; //loop counter for each line of the foliage
char date[9];

int main()
{
/*system("COLOR 79");*/

cout<< "Sam Bowen-Hughes. Tut 1T - Novemeber 2013";
cout << EOL << EOL;
cout << "Enter the size of the chirstmas tree you wish to build ";
getValidHeight();
cout << EOL;
drawBranches();
/*drawTrunk();*/
cout<< EOL;
system("PAUSE");

return(0);
}

void getValidHeight()
{
cin >> treeHeight;
while ((treeHeight < 4) || (treeHeight > 20))
{
cout << EOL << "The size of christmas tree you requested is out of stock." << EOL << "Please select another size: ";
cin >> treeHeight;
}
}

void drawBranches()
{
branchLine = 1;
while ( branchLine <= (treeHeight - 2))
{
drawABranch();
branchLine = branchLine +1;
}
}

void drawABranch()
{
for(branchLine = 1; branchLine <= (treeHeight - 2); branchLine++)
{
cout <<BLANK;
}
for(branchLine = 1; branchLine <= (treeHeight - 2); branchLine++)
{
cout <<LEAF;
cout<<EOL;
}

/*cout<<LEAF;
cout<<EOL;
*/
}


//void drawTrunk()
//{
//int trunkLine, spaces;
//trunkLine = 1;
//
//
//while (trunkLine <=2)
//{
// spaces = 1;
//
// while (spaces <= (treeHeight - 3))
//
// cout<<BLANK;
// spaces = spaces +1;
// //}
//
// cout<<WOOD;
// cout<<EOL;
// trunkLine = trunkLine +1;
//}
First of all please use code-tags next time.

Your problem is that your code.. well simply doesn't do what I guess you think it should be doing.
There is some weird stuff going on.

The problem lies within this function:
1
2
3
4
5
6
7
8
9
10
void drawABranch()
{
for(branchLine = 1; branchLine <= (treeHeight - 2); branchLine++) {
cout <<BLANK;
}
for(branchLine = 1; branchLine <= (treeHeight - 2); branchLine++) {
cout <<LEAF;
cout<<EOL;
}
}


When you enter the loop you initialize "branchLine" with 1 (so whatever it was before you call this function doesn't matter).
Then whatever comes next is just plain wrong.
It does something completly different from whatever you want to do.
I'll just be nice and give u a little piece of code how to draw your leafs the right way.

Rewitten code of the drawABranch-Funktion:
1
2
3
4
5
6
7
void drawABranch()
{
for(int i = 0; i < branchLine * 2 - 1; i++) {
cout << LEAF;
}
cout << EOL;
}


careful: untested!

How to draw the wood is something you have to figure out yourself now! ;)
i have created a very simple code for printing out a christmas tree

#include <iostream>
using namespace std;
int main () {

long long width, row=0, printed=0, i, j, l, r; /// l-left r-right
///TOP PART OF THE CHRISTMAS THREE
cout<<"Please input the width of the christmas three: ";
cin>>width;
if (width>80 or width<1) { /// this part checks if the input is valid or not
cout<<"Error. Invalid input."<<endl;
cout<<"Reasons may be:"<<endl;
cout<<"1. Inputed width is too big or too small."<<endl;
cout<<"2. Invalid input caused by inputing symbols different from numbers."<<endl;
return 1; /// if the input is not valid it stops the entire program
}
l=width/2+1;
r=width/2+1;
for (i=1;i<=width/2+1;i++) {
for (j=1;j<=width;j++) {
if (width%2==0) {
if (j>=l and j<=r-1) {
cout<<"@";
}
else {
cout<<" ";
}
}
if (width%2==1) {
if (j>=l and j<=r) {
cout<<"@"; /// printing out the "leaves" of the tree
}
else {
cout<<" "; /// printing out parts where there are no "leaves"
}
}
}
cout<<"\n";
r=r+1;
l=l-1;
}
///STEM OF THE CHRISTMAS THREE
while (row<width-(width/2+(width/3))) {
printed=0;
while (printed<width-(width-1)) {
for (i=1;i<=width/2-1;i++) {
cout<<" ";
}
if (width%2==0) {
if (printed==0 or printed==width-1 or row==0 or row==width-1) {
cout<<"||"; /// printing out the actual stem
}
}
else {
cout<<"||";
}
for (i=width/2+1;i<width-1;i++) {
cout<<" ";
}
printed=printed+1;
}
row=row+1;
cout<<"\n";
}

return 0; /// return 0 if code is completed
}

all you need to do is input the width and hit enter and BAM-you get your christmas tree. Im sorry for not using code tags. Im new to this site and i dont know how. I really hope this helped.
Last edited on
closed account (N36fSL3A)
http://www.cplusplus.com/articles/jEywvCM9/

That's how you use code tags.
Topic archived. No new replies allowed.