Hey guys I'm running into an error. I've got the tree and the base working. But now that I'm to the trunk it'll only center the first one and the rest are to the left. any suggestions?

#include <iostream>
using namespace std;

double height;

int main()
{
    cout <<"This program will take a height and create a tree.\n";
    cout <<"Enter your height (3-15): ";
    cin >> height;

    if (height < 3 || height > 15)
    {
        cout << "The number you entered is not between (3-15). Try again." << endl;
        return 0; 
    }

    //This will build the tree.

    for (int level = 0; level < height; level++)
    {
       for (int spaces = 0; spaces < height - level - 1; spaces++)
           cout << ' ';

        for (int a = 0; a < 2 - 1; a++)
           cout << '/';
        for (int middle = 0; middle < 2 * level; middle++)
            cout << ' ';
          for (int b = 0; b < 2 - 1; b++)
              cout << '\\' << endl;
    }  

    //This creates the base.

    {        
        for (int middle = 0; middle < 2 * height; middle++)

            cout << '-';
            cout << endl;
    }   

    //This creates the trunk.

    {
        for (int middle = 0; middle < height - 1; middle++)
           cout << ' ';

        for (int middle = 0; middle < 0.5 * height; middle++)
            cout << '|'<< '|' << endl;


    }
    return 0;
}

Recommended Answers

All 2 Replies

The last two loops should be nested:

for (int middle = 0; middle < 0.5 * height; middle++) {
    for (int middle = 0; middle < height - 1; middle++)
        cout << ' ';

    cout << '|'<< '|' << endl;
}

thanks that helped and solved this problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.