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>
usingnamespace std;
int main()
{
int i = 1;
int j = 1;
while (i <= 6)
{
while (j <= 6)
{
cout << "*" << endl;
j++;
}
i++;
}
return 0;
}
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.
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.
> 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.