Nested while loop

I need to make a while loop that produces the following pattern:
*
**
***
****
*****
******
I've tried using a nested loop, but I'm not doing it right or completely understanding it. Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
	int i = 1;
	int j = 1;
	while (i <= 6)
	{
		while (j <= 6)
		{
			cout << "*" << endl;
			j++;
		}
		i++;
	}
	return 0;
}
Last edited on
- Move the int j = 1 to inside the first while loop.
- do while (j <= i), not while (j <= 6)

And please edit your post format your code:
[code]

  { program here }

[/code]
Last edited on
Doing while(j<=1) gives me an infinite amount of astriks, and the program is supposed to do caps at 6. I'm still not getting the right input. Normally I would just do a for loop, but I'm required to do a while loop.
Last edited on
Hello kmcfall,


Normally I would just do a for loop


I hate to state the obvious, but as you said do the for loops first. Then comment them out and use it as a guide to write the while loops.

This should make Ganado's suggestion of moving int j = 1; inside the first while easier to understand.

What can be done with nested for loops can be done with nested while loops.

Andy
I just tried my instructions while on an actual computer. Works for me.
One thing I forgot was, don't print a newline after every *. Just print one after each row (inner loop).
If you're getting an infinite loop, perhaps you modified the outer loop instead of the inner loop. Since this code is relatively simple, try going through it by hand for n = 3 instead of 6.
Last edited on
A useful embellishment on all that is to make use of giving the variables (i and j) more meaningful names along the following lines:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;
int main()
{
    int row = 1;
    int column = 1;
    
    while (row <= 6)
    {
        column = 1; // <-- 
        while (column <= row) // <--
        {
            cout << "*" ; // << endl;
            column++;
        }
        cout << endl; // <--
        row++;
    }
    
    return 0;
}
> I've tried using a nested loop, but I'm not doing it right or completely understanding it.
When your program doesn't work, you need to start learning some debugging skills.

At the most basic level, just throwing in some extra cout statements will tell you a lot.
It's called software for a reason, you can change anything at any time, for any purpose.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
	int i = 1;
	int j = 1;
	while (i <= 6)
	{
		while (j <= 6)
		{
			cout << "*" << endl;
			j++;
			cout << "DEBUG inner j=" << j << endl;
		}
		i++;
		cout << "DEBUG outer i=" << i << endl;
	}
	return 0;
}

*
DEBUG inner j=2
*
DEBUG inner j=3
*
DEBUG inner j=4
*
DEBUG inner j=5
*
DEBUG inner j=6
*
DEBUG inner j=7
DEBUG outer i=2
DEBUG outer i=3
DEBUG outer i=4
DEBUG outer i=5
DEBUG outer i=6
DEBUG outer i=7

You can immediately see that something is wrong with your j while loop, because it only happens once.

When your programs get larger, it's better to start investigating your debugger options.
https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019
http://www.gdbtutorial.com/tutorial/how-use-gdb
Just being able to set a breakpoint and examine a variable will tell you a lot about how programs actually work.
kmcfall wrote:
I need to make a while loop that produces the following pattern:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
using namespace std;

int main()
{
   int i{};
   while( (++i) <= 6 ) cout << string( i, '*' ) << '\n';
}
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int i {1};
	while ((cout << setw(++i) << setfill('*') << '\n') && i <= 6);
}

Last edited on
Registered users can post here. Sign in or register to post.