954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help w/ for loop.

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;
	}
}


<< moderator edit: added code tags : [code][/code] >>

smallville
Newbie Poster
11 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

In your for loops

change

i=i++  
to 
i++

code will work fine

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 
#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

Daishi
Junior Poster in Training
80 posts since Aug 2005
Reputation Points: 10
Solved Threads: 2
 

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.

smallville
Newbie Poster
11 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

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

Daishi
Junior Poster in Training
80 posts since Aug 2005
Reputation Points: 10
Solved Threads: 2
 
#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?

smallville
Newbie Poster
11 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

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

Daishi
Junior Poster in Training
80 posts since Aug 2005
Reputation Points: 10
Solved Threads: 2
 

[QUOTE=smallville]

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

drock9975
Newbie Poster
16 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

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;
	}
}
smallville
Newbie Poster
11 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

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

Dulaithol
Newbie Poster
24 posts since Jan 2005
Reputation Points: 10
Solved Threads: 1
 

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.

smallville
Newbie Poster
11 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

Have you tried the setw(int), setfill, and/or alignment manipulators? These are usable if you include the and 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)

sifuedition
Light Poster
25 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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

drock9975
Newbie Poster
16 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

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.

smallville
Newbie Poster
11 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

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

drock9975
Newbie Poster
16 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

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

smallville
Newbie Poster
11 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

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');
}
smallville
Newbie Poster
11 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You