I am new to C++ and have a problem in a class I am taking.I am having trouble trying to create this the way my professor wants. this is what is required from the program:
Write a complete program that prints out a “tree” of asterisks with the number of lines specified by the user.
The following output is a five-line tree:
1 *
2 ***
3 *****
4 *******
5 *********

I have this as the loop;

for (i = 1; i <= lines; i++)
	{
	for (l = 1;l <= i; l++) // prints line number
	cout <<l;
	for(j = 1; j <= i; j++)  // calculation to print start
	cout <<"*";
	cout<<endl;
	}

It prints this
1
2
3
4
5
*
1
2
3
4
5
**
etc.. My question is can some one point me in the direction to get the loop to output like the first example.

Thanks

you need to combine this into two loop statements. try (i haven't tested this)

cout <<1 << "*";

in the second loop from the top

you need to combine this into two loop statements. try (i haven't tested this)

cout <<1 << "*";

in the second loop from the top

Thanks for quick reply
I made this mod and received this out out;
1*
1*1*2*2*
.
.
1*1*1*1*1*2*2*.....5*5*5*5*5*

If I break the loops apart, Execute 1 or the other only the output is this;
first for statement;
1
2
3
4
5

or for the second for statement;
*
**
***
*****
******
But when put back in nest they dont work.

sorry about that, only glancing at the code. For a start to make it easier to read , create variables with meaningful names it makes it easer to see what you are doing

sorry about that, only glancing at the code. For a start to make it easier to read , create variables with meaningful names it makes it easer to see what you are doing

This is not the complete set of code only the nested loop. I used "l" to represent lines and for the * I used j guess I could have used an "a" for Asterisk or "s" for splat. I tend to try and use meaningfull names but also like to keep them short.

Thanks for your suggestions I will relook the names.

Edited 6 Years Ago by Docbyte: n/a

try

for (i = 1; i <= lines; i++){
      cout <<i;
      for(j = 1; j <= i; j++) // calculation to print start
      cout <<"*";
      cout<<endl;
      }

try

for (i = 1; i <= lines; i++){
      cout <<i;
      for(j = 1; j <= i; j++) // calculation to print start
      cout <<"*";
      cout<<endl;
      }

Thanks that got it I had duplicated the line number loop

This question has already been answered. Start a new discussion instead.