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:
*
***
*****
***
*****
*******
*****
*******
*********