Home > Blockchain >  Spacing is incrementing for no reason when printing
Spacing is incrementing for no reason when printing

Time:09-11

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:

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

CodePudding user response:

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.

  • Related