Help with strings

I'm trying to make an algorithm to print a triangle of asterisks. For some reason, it sets the output asterisk as only one every time, instead of (maxShapeWidth - i) asterisks. Any help is appreciated.
Also, ignore the title. I'm not sure what exactly is causing the problem, and I didn't want to say something about a triangle algorithm.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
//-----
int main() {
    using namespace std;
    
    int maxShapeWidth = 10;
    int maxWidth = maxShapeWidth + 2;
    string hi;
    for (int i = maxShapeWidth; i > 0; i--) {
        if (i % 2 == 0) {
            string spaces(i/2, ' ');
            string stars = "*";
            if (i != maxShapeWidth) {
                string stars(maxShapeWidth - i, '*');
            }
            cout << maxShapeWidth - i;
            cout << "  " << spaces << stars << spaces << "  " << endl;
        }
    }
    return 0;
}
Although they have the same names, the strings at lines 13 and 15 are different. The one at line 15 gets destroyed at line 16. So try replacing line 15 with stars = string(maxShapeWidth - i, '*')
Oh, so it's like I'm just running computations without assigning them to a variable?
Also, line 11 should be 'if (i % 2 == 1) {"
Last edited on
Topic archived. No new replies allowed.