Hey guys,

I need to print out character symbols for this program. I need to print 2 different shapes of symbols. For the first shape, I have to print out a triangle, represented by the "*" in the nested for loop. I need to print out a square shape using "&" symbols but using a nested while loop. The target is such that if n=3, the output will be as follows:

*
&
*
**
&&
&&
*
**
***
&&&
&&&
&&&

I've tried placing the nested while loop inside the for loop, but all it does is an infinite loop :confused:

``````#include <iostream>

using namespace std;

int main()
{
int n, i, j;

do{
cout <<" Enter a positive int n: ";
cin >> n;
cout << endl;
} while (n < 0 && n > 10);

do{
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
cout << "*\t";
cout << endl;

}

} while (i <= n);

}``````

Thank you for your help :)

Try using another variable, say k, to keep track of the current "size" of the shape whereas n is the maximal size of the shape. Set k = 1 before the second do while loop, change the i <= n to i <= k in the for loop, increment k …

Try writing this a piece at a time.
1) Create the pattern
*
**
***
2) Put that in a loop to make
*

*
**

*
**
***

3) Make the pattern
&
&&
&&&
4) Add a loop to make
\$

\$\$
\$\$

## All 5 Replies

Try using another variable, say k, to keep track of the current "size" of the shape whereas n is the maximal size of the shape. Set k = 1 before the second do while loop, change the i <= n to i <= k in the for loop, increment k afte completion of the nested for loops and then check that k < n in the while conditional.

This is my code so far. I need the code to print this out, if n=3
*
&
*
**
&&
&&
*
**
***
&&&
&&&
&&&

but the code currently prints this out:
*
**
***
&&&
&&&
&&&

``````#include <iostream>

using namespace std;

int main()
{
int n, i;
int j, k;

do{
cout <<" Enter a positive int n: ";
cin >> n;
cout << endl;
} while (n > 0 && n > 10);

do{
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
cout << "*\t";
cout << endl;

}

for (int width=1; width <= n; ++width)

{
for (int width2=1 ; width2 <=n; ++width2)
{
cout << "&\t"   ;
}
cout << endl;
}

} while (i <= n);

}``````

I have modified the inner for loops to this.

So now it prints out like this when n=3.

*
&
**
&&
&&
***
&&&
&&&

but I want this
*
&
*
**
&&
&&
*
**
***
&&&
&&&
&&&

``````for (i = 1; i <= n; i++)
{

for (j = 1; j <= i; j++)
cout << "*\t";
cout << endl;

for (int l=1; l <= i; l++)

{
for (int m=1 ; m <=i; m++)
{
cout << "&\t";
}
cout << endl;
}

}``````

Try writing this a piece at a time.
1) Create the pattern
*
**
***
2) Put that in a loop to make
*

*
**

*
**
***

3) Make the pattern
&
&&
&&&
4) Add a loop to make
\$

\$\$
\$\$

\$\$\$
\$\$\$
\$\$\$

5) Merge all the pieces to create the final pattern. You might have to plan the pattern and code with pencil and paper to get the pattern correct. Don't just try to create it while sitting at the computer. Good programmers to a lot of non-computer work first to plan out the code.

Thanks for your help! I finally figured it out!
Here's my code :D

``````for (i = 1; i <= n; i++)
{

{

for (j=1; j <= i; j++)

{
for (k=1 ; k <=j; k++)
{
cout << "*\t";

}

cout << endl;

}
for (int l=1; l <= i; l++)

{
for (int m=1 ; m <=i; m++)
{
cout << "&\t";
}
cout << endl;
}
}
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.