-1

I'm trying to print a Christmas tree which would look like. There is an only issue with spacing in front of the leaves if I input 1 it looks fine but for anything above that the spaces increase by 1.

#include <iostream>
#include <iomanip> 
using namespace std;

void pineT (int rows , int finish  , int spaces) {
  
    int space; // initialize variables.
    string total_space = "";
    string stars = "";
    string all_images = "";

    for(int i = rows, k = 0; i <= finish; ++i, k = 0) // getting rows for requested range.
    {
        for(space = 1; space <= spaces -i; ++space) // intial space 
        {
            total_space += " ";
        }

        while(k != 2*i-1) // printing stars per row.
        {
            stars += "*";
            ++k;
        }
      
        all_images += total_space; // one row.
        all_images += stars + "\n";
      
        stars = "";
        total_space = "";
    }

    cout<< all_images;  // final product.
}


int main() {
    string total_spaces = "";
    int start = 1, finish = 3;  // intial tree layer increases per increment.
    int rows;
    cin >> rows;
    int intial_spaces =rows*2 +1;
    int spaces = intial_spaces;

    // printing top half of tree.
    while ( finish != rows+3) { // To print multple layers.
        pineT (start , finish , spaces);
        start +=1 ;
        finish +=1;
        if ((start > rows)) {
            spaces-= start;
        }
    }

    return 0 ;
}

output goal with an input of 2: The top half is just shifted by 1 and when I input 3 it shifts by 2.

   *
  ***
 *****
  ***
 *****
*******

Current output when input 2:

    *
   ***
  *****
   ***
  *****
 *******

Current output when input 3:

      *
     ***
    *****
     ***
    *****
   *******
    *****
   *******
  *********
  • It would probably be helpful if you showed us what the output should look like with the input values `1`, `2` and `3`. Otherwise, there is a danger that we may give you a solution that only works with the input value `2`, but not `1` and `3`. – Andreas Wenzel Sep 10 '22 at 04:52
  • You're writing `initializing variables` but still not actually initializing `space`. Moreover, there is no point in initializing `std::string` with empty string `""`. Instead you should initialize built in types. – Jason Sep 10 '22 at 04:56
  • @JasonLiam: I guess the code comment `//initialize variables` is supposed to refer to the code on the next 3 lines, not the code on the current line. I agree that this comment is misleading. However, I see no problem with not actualy initializing `space`, because a value is assigned to it before it is used. – Andreas Wenzel Sep 10 '22 at 05:12
  • Have you tried running your code line-by-line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Sep 10 '22 at 05:17

1 Answers1

1

The first thing to do to find the problem is to determine whether the problem is in the function main or in the function pineT.

When running your program line by line in a debugger, you will determine that when rows == 1, then main will call the function pineT once, like this:

pineT( 1, 3, 3 );

When rows == 2, then main will call the function pineT twice, like this:

pineT( 1, 3, 5 );

and

pineT( 2, 4, 5 );

Since you did not specify what the function pineT should do exactly, I cannot tell you whether the function pineT does not work as intended or whether the problem is how the function main is calling pineT.

However, according to my tests, the function call

pineT( 1, 3, 3 );

has the following output:

  *
 ***
*****

The function call

pineT( 1, 3, 5 );

has the following output:

    *
   ***
  *****

And the function call

pineT( 2, 4, 5 );

has the following output:

   ***
  *****
 *******

Using this information, it should be possible for you to determine whether the function pineT does not work as intended, or whether it is the function main that does not work as intended.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39