Nested loops not incrementing

Hi I am trying to make a nested loop that builds a triangle with the height based on user input. The trouble is none of my for loops seem to be incrementing. I am using DEV C++ and here is my code. I want to understand why so please don't just post code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int rows;         //rows
    int spaces;      //spaces
    int ast;           //asterisk
    int height;      //height input from user   
    
    cout << "How big do you want your triangle?" << endl;
    cout << "Please enter the height now. (Positive integer only): ";
    cin >> height;
    cin.ignore();

    for (rows=1; rows<=height; rows++)
    {
        for (spaces=height; spaces>0; spaces--)
        {
            cout << " ";
        }
        for (ast=1; ast<=height; ast++)
        {
            cout << "*";
        }
        cout << endl;
        //(2*ast)+1;      //increases the width of the triangle
    }
    
    cin.get();    

return 0;

}


I have made the corrections suggest but my loops still don't increment. Here is the output I am getting

*****
*****
*****
*****
*****
Last edited on
I will assume that you have the following at the top of your code and that you just left it out:

include <iostream>

using namespace std;

Aside from that you are declaring some of your variables twice. On line 13, for e.g., you write "int rows = 1;" whereas on line 3 you write "int rows." You can delete the line on 3 because it's redundant since you're creating a variable on line 13 that goes by the same name.

You can delete line 5 because it's redundant and doesn't do anything. on line 19 you're creating a variable "ast" and setting it equal to 1.
Take a look at line 24, you are saying as long as ast is equal heightto print an *. That is exactly what it is doing, height never changes in any of your for statements.

try somthing like this for line 24

for (ast=1; ast<=rows; ast++)

I dont know if this is they way that you want your triangle to be shaped but it should put you back on the right path.

Topic archived. No new replies allowed.