| | |
Help w/ for loop.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2005
Posts: 11
Reputation:
Solved Threads: 0
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:
<< moderator edit: added code tags: [code][/code] >>
*
**
***
****
*****
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)
#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; } }
•
•
Join Date: Aug 2005
Posts: 80
Reputation:
Solved Threads: 2
C++ Syntax (Toggle Plain Text)
#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
•
•
Join Date: Aug 2005
Posts: 80
Reputation:
Solved Threads: 2
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

-Fredric
•
•
Join Date: Oct 2005
Posts: 11
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#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?
•
•
Join Date: Jun 2005
Posts: 16
Reputation:
Solved Threads: 1
[QUOTE=smallville]
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
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: Oct 2005
Posts: 11
Reputation:
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.
: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)
#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; } }
![]() |
Similar Threads
- Help with gui loop. (C)
- Loop...without the loop (Java)
Other Threads in the C++ Forum
- Previous Thread: private inheritance
- Next Thread: returning arrays from functions
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






****