creating pattern

Hi, how to create a diagonal pattern by the given function

void diagonal(int size, char op)

The function takes in 2 arguments, size and op and displays a diagonal line of op char. for example, diagonal (5,#) will produce the following output.

#
#
#
#
#
use nested for loop
@wwwiii:

Why would you need a nested for loop?

@j2013:

Simply create a string and, for each iteration, append a space to it within a for loop. Here's an example:

1
2
3
4
5
6
7
8
9
10
void diagonal(int size, char op)
{
	std::string spaceCount = "";
	
	for (int i = 0; i < size; i++)
	{
		std::cout << spaceCount << op << std::endl;
		spaceCount.append(" ");
	}
}


You'll need to include the <string> header to output strings to the console.
Sorry, it doesn't print diagonal
You obviously messed something up.

It works perfectly fine for me.
Topic archived. No new replies allowed.