-1

I'm currently self teaching C++, and have a problem. the purpose of this code is to print asterisks in a pyramiding fashion, with the example being if the input (variable int n) is 5, it should print like this:

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

Here's the code:

#include <iostream>

using namespace std;

int main()
{
    int i, n;
    cout << "What is your number?" << endl;
    cin >> n;
    cout << "n is: " << n << endl;
    int arr[n];
    int z = n;
    while(z > 0){
        arr[z] = 0;
        z--;
    }
    z = n;
    for(int y = 0; y<=z; y++){
        arr[y] = z;
        cout << "Y is: " << y << endl;
        cout << "Arr[Y] is: " << arr[y] << endl;
        cout << "z is: " << z << endl;
        z--;
    }
    while(n > 0){
        int x = arr[n];
        while(x > 0){
            cout << "*";
            x--;
        }
        cout << endl;
        n--;
    }
}

but the first half (rounded up) will always be blank on printing. I don't know how to debug in CodeBlocks yet, so I can't tell you what's hiding in the memory to solve this myself

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • 9
    Why do you think you need an array? – Yksisarvinen Nov 23 '20 at 15:33
  • 11
    *I don't know how to debug in CodeBlocks yet* Now is the perfect time to learn. – NathanOliver Nov 23 '20 at 15:33
  • 1
    what is that "Y is: ", "Arr[y] is:" etc. ? If that is not related to the question perhapse remove it from the code. If it is you should state it also in the desired output. Details do matter. – 463035818_is_not_an_ai Nov 23 '20 at 15:33
  • 2
    If you put the printing loop inside the counting loop, you can get rid of the array. This will simplify matters greatly. – molbdnilo Nov 23 '20 at 15:40
  • You're indexing outside the array, which has undefined behaviour. (You are probably overwriting `n`.) – molbdnilo Nov 23 '20 at 15:47
  • 2
    As a side note, `int arr[n];` won't work because `n` must be defined at compile time and you define `n` at runtime. A workaround is to use a pointer to a dynamic allocated array: `int *arr = new int[n];` And dont forgot to `delete [] arr;` at the end. – fern17 Nov 23 '20 at 15:51
  • [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/430766) – bitmask Nov 23 '20 at 16:07

2 Answers2

2

Although this does not fix your code, you stated that you are currently learning C++, so I think providing a better solution is even much better.

Here is another simpler way to achieve your goal:

#include <iostream>

using namespace std;

int main()
{
    unsigned int i, n;

    cout << "What is your number?" << endl;
    cin >> n;

    for (auto i = 0; i < n; i++)
    {
        for (auto j = 0; j <= i; j++)
        {
            cout << "*";
        }
        cout << endl;
    }
}

Remember, trying to implement an easy, simple and efficient way is always better for performance, readability and debugging. Good luck in your learning and happy coding!

XDarkDev
  • 46
  • 1
  • 8
0

The most easiest way to do this

#include <iostream>

using namespace std;

int main() {

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cout << string(i, '*') << endl;
  }  

  return 0;
}
starboy_jb
  • 899
  • 1
  • 6
  • 13