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;
1 2 3 4 5 6 7 8 9 10 11 12 13
for(i = 1; i <= lines; i++)
{
for (int l = 1; l <= lines; l++) // prints line number
cout << l <<endl;
cout<<endl;
{
for (k = 1; k <= i; k++) // calculation to print spaces
cout << " ";
}
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.
You can easily do this program with just two for loops total. One is inside the other. If your program has more than two for loops, you're probably making a mistake.
Also, you don't need to use endl unless you want to skip to the next line. You should only have one endl in the entire program.