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 :)

Dani AI

Generated

A simple, reliable way to produce the requested output is to treat the overall task as "for size = 1..n: print a star triangle of height size, then print an ampersand square of size×size." That is the approach 's suggestion (use a separate counter) points toward, and it follows 's advice to plan the pieces first. , your final nested loops do the job — the remaining improvements are clarity, correct input validation, and avoiding subtle brace/scope bugs that cause infinite loops.

Below is a compact, readable implementation that uses clear loop variable names and the std::string constructor to avoid an extra inner character loop:

#include <iostream>
#include <string>
#include <limits>

using namespace std;

int main() {
    int n;
    do {
        cout << "Enter a positive int n (1..10): ";
        if (!(cin >> n)) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); continue; }
    } while (n <= 0 || n > 10);

    for (int size = 1; size <= n; ++size) {
        for (int row = 1; row <= size; ++row)
            cout << string(row, '*') << '\n';
        string amps(size, '&');
        for (int r = 0; r < size; ++r)
            cout << amps << '\n';
    }
    return 0;
}

Troubleshooting notes: use || (or) for out-of-range input checks (not &&), reset or limit loop counters inside the scope where they’re used, and put braces around multi-statement loop bodies so a stray cout isn’t executed in the wrong place. Prefer '\n' over endl unless you need the flush. Finally, avoid mixing tabs and spaces for alignment — fixed-width characters or a string-based approach (shown above) keeps the shapes consistent across terminals.

Recommended Answers

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
*
&
*
**
&&
&&
*
**
***
&&&
&&&
&&&

thanks for your help

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.