Help w/ for loop.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2005
Posts: 11
Reputation: smallville is an unknown quantity at this point 
Solved Threads: 0
smallville smallville is offline Offline
Newbie Poster

Help w/ for loop.

 
0
  #1
Oct 6th, 2005
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:

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int i, j, lines;
  8.  
  9. cout << "This Program creates Christmas Trees." << endl;
  10.  
  11. do
  12. {
  13. cout << "Please enter a number between 1 and 30" << endl;
  14. cin >> lines;
  15. }while (lines < 1 || lines > 30);
  16.  
  17. for(j=1; j<=lines; j++)
  18. {
  19.  
  20. for(i=1; i<=40; i=i++)
  21. {
  22. cout << " ";
  23. }
  24.  
  25. for(i=1; i<=j; i=i++)
  26. {
  27. cout << "*";
  28. }
  29. cout << endl;
  30. }
  31. }
<< moderator edit: added code tags: [code][/code] >>
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Help w/ for loop.

 
0
  #2
Oct 6th, 2005
In your for loops

change
  1. i=i++
  2. to
  3. i++
code will work fine
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: Help w/ for loop.

 
0
  #3
Oct 6th, 2005
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int i, j, lines;
  8.  
  9. cout << "This Program creates Christmas Trees." << endl;
  10.  
  11. do
  12. {
  13. cout << "Please enter a number between 1 and 30" << endl;
  14. cin >> lines;
  15. }while (lines < 1 || lines > 30);
  16.  
  17. for(j=0; j<lines; j++)
  18. {
  19. for(i=0; i<=j; i++)
  20. {
  21. cout << "*";
  22. }
  23. cout << endl;
  24. }
  25. }

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
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 11
Reputation: smallville is an unknown quantity at this point 
Solved Threads: 0
smallville smallville is offline Offline
Newbie Poster

Re: Help w/ for loop.

 
0
  #4
Oct 6th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: Help w/ for loop.

 
0
  #5
Oct 7th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 11
Reputation: smallville is an unknown quantity at this point 
Solved Threads: 0
smallville smallville is offline Offline
Newbie Poster

Re: Help w/ for loop.

 
0
  #6
Oct 7th, 2005
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int i, j, lines;
  8.  
  9. cout << "This Program creates Christmas Trees." << endl;
  10.  
  11. do
  12. {
  13. cout << "Please enter a number between 1 and 30" << endl;
  14. cin >> lines;
  15. }while (lines < 1 || lines > 30);
  16.  
  17. for(j=1; j<=lines; j++)
  18. {
  19.  
  20. for(i=1; i<=lines; i++)
  21. {
  22. cout << " ";
  23. }
  24.  
  25. for(i=1; i<=j; i++)
  26. {
  27. cout << "*";
  28. }
  29. cout << endl;
  30. }
  31. }

I tried but I'm still getting the same effect. What am I doing wrong? Is the second loop the problem now?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: Help w/ for loop.

 
0
  #7
Oct 7th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 16
Reputation: drock9975 is an unknown quantity at this point 
Solved Threads: 1
drock9975 drock9975 is offline Offline
Newbie Poster

Re: Help w/ for loop.

 
0
  #8
Oct 9th, 2005
[QUOTE=smallville]
  1.  
  2.  
  3. for(j=1; j<=lines; j++)
  4. {
  5.  
  6. for(i=1; i<=lines; i++)
  7. {
  8. cout << " ";
  9. }
  10.  
  11. for(i=1; i<=j; i++)
  12. {
  13. cout << "*";
  14. }
  15. cout << endl;
  16. }
  17. }

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
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 11
Reputation: smallville is an unknown quantity at this point 
Solved Threads: 0
smallville smallville is offline Offline
Newbie Poster

Re: Help w/ for loop.

 
0
  #9
Oct 11th, 2005
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.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int i, j, lines;
  8.  
  9. cout << "This Program creates Christmas Trees." << endl;
  10.  
  11. do
  12. {
  13. cout << "Please enter a number between 1 and 30" << endl;
  14. cin >> lines;
  15. }while (lines < 1 || lines > 30);
  16.  
  17. for(j=1; j<=lines; j++)
  18. {
  19.  
  20. for(i=1; i<=lines; i++)
  21. {
  22. cout << " ";
  23. }
  24.  
  25. for(i=1; i<=j; i++)
  26. {
  27. cout << "*";
  28. }
  29. cout << endl;
  30. }
  31. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 24
Reputation: Dulaithol is an unknown quantity at this point 
Solved Threads: 1
Dulaithol Dulaithol is offline Offline
Newbie Poster

Re: Help w/ for loop.

 
0
  #10
Oct 11th, 2005
Try putting in "\s" instead of " " but that might just be a php thing.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC