I'm trying to put together this program that creates a for loop but my end result is not positioned the way I need it like this but flipped around so it looks like a christmas tree effect.

*
**
***
****
*****

Please help I'm still learning and I can't figure it out for the life of me!

Source code:

#include <iostream>

using namespace std;

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

    cout << "This Program creates Christmas Trees." << endl;

    do
    {
        cout << "Please enter a number between 1 and 30" << endl;
        cin  >> lines; 
    }while (lines < 1 || lines > 30);

    for(j=1; j<=lines; j++)
    {

       for(i=1; i<=40; i=i++)
       {
          cout << " ";
       }

       for(i=1; i<=j; i=i++)
       {
          cout << "*";
       }
       cout << endl;
    }
}

Recommended Answers

All 16 Replies

In your for loops

change

i=i++  
to 
i++

code will work fine

#include <iostream>

using namespace std;

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

  cout << "This Program creates Christmas Trees." << endl;

  do
    {
      cout << "Please enter a number between 1 and 30" << endl;
      cin  >> lines;
    }while (lines < 1 || lines > 30);

  for(j=0; j<lines; j++)
    {
      for(i=0; i<=j; i++)
        {
          cout << "*";
        }
      cout << endl;
    }
}

You were really close. :) Just take out the first for loop, and the i=i++ is something you *do not* want to do (think about it).

-Fredric

thanks, it works but it's showing like in the post... however I need it to look more like an equal triangle from the top down

**********

the reason the other one was there is to bring it to the center of the program. The extra spaces it what brought it to the middle.. I'm completely stuck.

Err', sorry, reread your post more carefully..if you want a christmas tree effect, then you need to include that for loop I said to leave out, but set the initial value of i to j in it, change 40 to lines, and increment i correctly. Then you have to make one little change to the second for loop and you should get a Christmas tree. Ok, no more hints! :)

-Fredric

#include <iostream>

using namespace std;

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

    cout << "This Program creates Christmas Trees." << endl;

	do
	{
		cout << "Please enter a number between 1 and 30" << endl;
		cin  >> lines; 
	}while (lines < 1 || lines > 30);

	for(j=1; j<=lines; j++)
	{

	   for(i=1; i<=lines; i++)
	   {
	      cout << " ";
	   }
	   
	   for(i=1; i<=j; i++)
	   {
		  cout << "*";
	   }
	   cout << endl;
	}
}

I tried but I'm still getting the same effect. What am I doing wrong? Is the second loop the problem now?

I assume you have the left side of the christmas tree. What you need is the right side? You just need to print out twice as many stars in that case.

0: *
1: ***
2:*****

-Fredric

for(j=1; j<=lines; j++)
	{

	   for(i=1; i<=lines; i++)
	   {
	      cout << " ";
	   }
	   
	   for(i=1; i<=j; i++)
	   {
		  cout << "*";
	   }
	   cout << endl;
	}
}

the problem is with the for loops and the numbers you are using for the test program

if the first for loop
for(i=1; i<=lines; i++)
{
cout << " ";
}

the number for lines never changes so you are always moving over the same amount of spaces before printing out your stars... try making a temp variable that starts off equal to lines but decrements after both for loops are executed

the second for loop you want to print out stars that are double the amount you currently are; however, if you just simply attempt to print out double stars you will get a top of the tree that looks like this **
so you don't want just 2*j

man I've been racking my brains trying to fix this program without having to post so I would learn it but I have tried so many different ways in doing it and it still doesn't work for me :( :cry:

* ** *** **** *****

I need it to not double but to add one to each line for however many lines you are asking the program to create.

#include <iostream>

using namespace std;

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

    cout << "This Program creates Christmas Trees." << endl;

	do
	{
		cout << "Please enter a number between 1 and 30" << endl;
		cin  >> lines; 
	}while (lines < 1 || lines > 30);

	for(j=1; j<=lines; j++)
	{

	   for(i=1; i<=lines; i++)
	   {
	      cout << " ";
	   }
	   
	   for(i=1; i<=j; i++)
	   {
		  cout << "*";
	   }
	   cout << endl;
	}
}

Try putting in "\s" instead of " " but that might just be a php thing.

that just creates /S looping x number of times

professor added the space in there to move the "tree" over to the middle of the screen so to speak.

Have you tried the setw(int), setfill, and/or alignment manipulators? These are usable if you include the <iomanip> and <iostream> headers. Try searching on them and see if you find what you need (I will bet this has been asked in this forum before if you have the time to search it properly)

man I've been racking my brains trying to fix this program without having to post so I would learn it but I have tried so many different ways in doing it and it still doesn't work for me :( :cry:

* ** *** **** *****

I need it to not double but to add one to each line for however many lines you are asking the program to create.

#include <iostream>

using namespace std;

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

    cout << "This Program creates Christmas Trees." << endl;

	do
	{
		cout << "Please enter a number between 1 and 30" << endl;
		cin  >> lines; 
	}while (lines < 1 || lines > 30);

        
	for(j = 0; j < lines; j++)
	{

	   for(i=0; i < lines - j; i++) 
	   {
	      cout << " ";
	   }
	   
	   for(i = 0; i < (j*2-1); i++)
	   {
		  cout << "*";
	   }
	   cout << endl;
           
	}
}

i made a couple of changes to the code... try that and let me know if it works

if you don't put a temp variable that starts equal to lines and decrements after every go around you will always move over and start your tree at the same amount of spaces over

you need to print out double the amount of stars to make a full tree but simply printing double will give you a ** at the top of you tree... if you double and subtract by 1 you should get a nice tree

according to the professor we should not need to use an alignment manipulator. I guess my for loop is incorrect. I just can't figure out where. I've tried setting i to 2 but that doesn't work either.

i just altered it again... try that

i didn't use a temp variable and it should print a tree... i didn't test it but it should do the trick

This is the result... lines are scattered

This Program creates Christmas Trees.
Please enter a number between 1 and 30
5

*
***
*****
*******
*********
Press any key to continue

alright I got it to work. I decided to start over and put a while statement in there... Here's the code, with so much diligence I even added whether or not he would want another tree since it took me so long I thought he might want 2 or 3 or whatever! LOL Thanks for your help though!

#include <iostream>

using namespace std;

int main()
{
	int i, j, lines;
	char choice;

	do
	{
		//Input validation
		do
		{
			cout << "Please enter number of lines between 1 and 30." << endl;
			cin  >> lines;
		}
		while (lines < 1 || lines > 30);

		//Print Trees
		system("cls");
		for (j=1; j<=lines * 2; j=j+2)
		{
			for (i=j; i<=80; i=i+2)
			{
				cout << " ";
			}
			for (i=1; i<=j; i++)
			{
				cout << "*";
			}
			cout << endl;
		}
		cout << endl << endl;

		cout << "Another tree?(Y/N)" << endl;
		cin  >> choice;
	}
	while (choice == 'Y' || choice == 'y');
}
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.