Nested loop Triangle of stars

So the problem is to use nested for loops statements to draw triangles of "*"s. The number of "*"s on the last row is input from the user (valid range: 5 to 21).
Sample dialog:
Drawing triangles program.
How many triangles? 2
How many stars/last row (5-21)? 25
Out of range. Reenter: 7
*
**
***
****
*****
******
*******
How many stars/last row (5-21)? 5

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

however when i use
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
int stars=1;
for(int line=0; line < 3; line++)
{
for(int s=0; s < stars; s++)
cout << "*";
stars+=2;
cout << endl;
}
return(0);
}


my output is
*
***
*****

how do i get it to look like the example? is there another code I should use??
line 12

stars+=2;

will make 2 more stars.. so.. it should be

stars++;
or
stars+=1;

:D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
int stars=1;
for(int line=0; line < 3; line++)
{
for(int s=0; s < stars; s++)
cout << "*";
stars++; // or stars+=1;
cout << endl;
}
return(0);
}
Last edited on
It only takes one [seemingly innocent] question like that to declare that you didn't do your own homework. Sure, I could be wrong...

Here's my guess:
http://answers.yahoo.com/question/index?qid=20100418154800AATEUhI
Ur actually right I didn't do it by myself I got help from some one who couldn't answer the question this thread is about and neither did u so ur point of responding was for what? Because u were bored I assume. Regardless of your reasoning you have a wonderful day.
Sorry duplicate below
Last edited on
Did you try my code? Let me know if it works for you.
Last edited on
@ OP: moorecm is right in that we don't post answers to problems that seem like they are school work. There may be a few differences in philosophy here but pretty much universally the go-to guys agree on this.

In fact: http://www.cplusplus.com/forum/articles/31015/
The point of my post is not to cause a fuss but rather to make it clear that we (go-to guys?) were not born yesterday. We are more than willing to help but often enough posters try to take advantage.
all i needed is help with was basically what do i need to do, for a users input to make the last row of the triangle. So i came here to see if anyone can help me, NO i dont know the code,and NO i dont know where to start looking for it at.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
int stars=1;
for(int line=0; line < 3; line++)
{
for(int s=0; s < stars; s++)
cout << "*";
stars++; // or stars+=1;
cout << endl;
}
return(0);
}
this is what i was looking for< thanx for your help any way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main()
{
int stars=1, num ;
cout << "How many triangles?" << endl;
cin >> num;
for(int q=1; q<=num; q++)
{
cout << "How many stars in the last row (5-21)" <<endl;
cin >> stars;

for(int line=1; line <= stars; line++)
{
for(int s=1; s <= line; s++)
cout << "*";
//stars+=1

cout << endl;
}

}

return 0;
}
Topic archived. No new replies allowed.