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;
}

Dani AI

Generated

The symptom (only the first trunk line centered) comes from printing the leading spaces once instead of for every trunk row. was right to suggest moving the spaces print inside the trunk loop — that fixes the immediate bug. Below are a few small, robust refinements to make the output consistent and the code easier to reason about.

Prefer an integer height and compute trunk size with integer arithmetic so rounding is explicit. Compute the number of trunk rows and the left padding from the tree width (which is 2 * height) and print spaces for each trunk line. Example trunk-only snippet:

int trunkRows = (height + 1) / 2;   // ceil(height/2)
int trunkWidth = 2;                 // choose 1 or 2 for visual style
int leftSpaces = (2 * height - trunkWidth) / 2;

for (int r = 0; r < trunkRows; ++r) {
    for (int s = 0; s < leftSpaces; ++s) cout << ' ';
    for (int w = 0; w < trunkWidth; ++w) cout << '|';
    cout << '\n';
}

Other small tips: use cout << string(2 * height, '-') << '\n'; to draw the base in one statement, always use braces for loops with multiple statements to avoid indentation bugs, and avoid reusing the same loop-variable name in nested loops. As confirmed, the nested-loop approach from fixes the centering; the snippets above just make the behavior predictable for odd/even heights and reduce subtle floating/integer conversion issues.

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.