944,196 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6903
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 6th, 2005
0

Help w/ for loop.

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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] >>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smallville is offline Offline
11 posts
since Oct 2005
Oct 6th, 2005
0

Re: Help w/ for loop.

In your for loops

change
C++ Syntax (Toggle Plain Text)
  1. i=i++
  2. to
  3. i++
code will work fine
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Oct 6th, 2005
0

Re: Help w/ for loop.

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Daishi is offline Offline
80 posts
since Aug 2005
Oct 6th, 2005
0

Re: Help w/ for loop.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smallville is offline Offline
11 posts
since Oct 2005
Oct 7th, 2005
0

Re: Help w/ for loop.

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
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Daishi is offline Offline
80 posts
since Aug 2005
Oct 7th, 2005
0

Re: Help w/ for loop.

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smallville is offline Offline
11 posts
since Oct 2005
Oct 7th, 2005
0

Re: Help w/ for loop.

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
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Daishi is offline Offline
80 posts
since Aug 2005
Oct 9th, 2005
0

Re: Help w/ for loop.

[QUOTE=smallville]
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
drock9975 is offline Offline
16 posts
since Jun 2005
Oct 11th, 2005
0

Re: Help w/ for loop.

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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
smallville is offline Offline
11 posts
since Oct 2005
Oct 11th, 2005
0

Re: Help w/ for loop.

Try putting in "\s" instead of " " but that might just be a php thing.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Dulaithol is offline Offline
24 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ vector question?
Next Thread in C++ Forum Timeline: returning arrays from functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC