0

I am trying to create a staircase pattern based on an input integer. (Ex:If I put 6 as my input, the output would be as follow):

     #
    ##
   ###
  ####
 #####
######

the height and the width of the triangle is 6. I have attempted what I thought could replicate this pattern(below):

//first method
void staircase(int n){
std::string mystring;
   int j=1;
   int k=0;
   for (int i=0;i<n;i++){
       while(j<(n-1-i)){
           mystring+=" ";
           j++;
       }
       while(k<(i+1)){
           mystring+="#";
           k++;
       }
       cout<<mystring<<endl;
    }
}

//second method
void staircase(int n) {
    std::string mystring;
    int j=1;
    int k=0;
    char blank=' ';
    char sign='#';
    for (int i=0;i<n;i++){
        while(j<(n-1-i)){
            mystring.push_back(blank);
            j++;
        }
        while(k<(i+1)){
            mystring.push_back(sign);
            k++;
        }
        cout<<mystring<<endl;
    }


}

both of the method return the output as followed:

#
##
###
####
#####
######

At first, I thought the first while loop got skipped, but it was not the case when I attempt to put a variable inside the first while loop to test the output. Does anyone have an explanation? Am I missing something crucial? It is because this seems like a fairly simple problem, but I couldn't get it to work for the past hour.

James
  • 57
  • 3
  • _"when I attempt to put a variable inside the first while loop to test the output"_ -- I don't know what exactly this test was, but it was probably flawed. Try sticking the line `cout<<'j';` in the first loop and `cout<<'k';` in the second. If your loops are working as intended, each line should start with as many `j`'s as there are spaces, and as many `k`'s as there are pound signs. – JaMiT Oct 26 '19 at 06:13
  • Not exactly a duplicate, but highly relevant: [Initializing variable in loop](https://stackoverflow.com/questions/13186476/initializing-variable-in-loop) – JaMiT Oct 26 '19 at 06:18

2 Answers2

0

I think some confusion in your logic: please try below:

void staircase(int length) {
int n=length;
    for (int i=0;i<=n;i++)
    {
        for(int j=n;j>=0;j--)
        {
            if(j<i)
            {
                cout<<"#";
            }
            else
            {
               cout<<" "; 
            }

        }
        cout<<endl;
    }
}
Divyesh patel
  • 967
  • 1
  • 6
  • 21
0

There are some minor flaws in your code. Let me help you correct them:

#include <iostream>

using namespace std;

void staircase(int n){
    std::string mystring;
    int j=1;
    int k=0;
    // the intention of this for loop is to get the string of #'s for each line
    for (int i=0;i<n;i++) {
        // so for each line we need to make this empty(otherwise you are carrying the older one)
        mystring = "";
         // j and k are for indexing the position of the staircase, so they must be made 0 for each loop iteration
        j = 0; k = 0;
        // both these loops are good
        while(j<(n-1-i)){
            mystring+=" ";
            j++;
        }
        while(k<(i+1)){
             mystring+="#";
             k++;
        }
        cout<<mystring<<endl;
    }
} 

Same is the case for the second method also, you need to make the string to empty and j and k to 0.

Hope this helps,

THANKS, Rajkumar

  • thank you very much! I forgot to put an empty string before concatenate the blank space and the sign. – James Oct 26 '19 at 16:44