0

I need to print this triangle:

* ** *** **** ***** ****** ******* ******** 

using a FOR and WHILE loop. I need help, I have already figured out the for loop version I just have to convert it to while loop but everything I try is not giving me the correct output! Any help is appreciated!

My code so far:

#include <iostream> using namespace std; int main(int argc, char**argv) { int i = 1; int j = 1; int N = 8; while (i <= N) { i = i++; while(j <= i) { cout<<"*"; j = j++; } cout<<endl; } } 
4
  • this is the triangle, i don't know if it showed up (right triangle) * ** *** Commented Aug 26, 2011 at 20:13
  • 4
    Is this homework? If it is, add the homework tag. Commented Aug 26, 2011 at 20:15
  • 3
    you don't need i = i++ and j = j++. just i++ and j++ will do the trick. Commented Aug 26, 2011 at 20:17
  • 1
    j = j++ says "increment j, then assign j the value it had before incrementing it." Probably not what you want. Commented Aug 26, 2011 at 20:20

6 Answers 6

3

I'll give you a hint (in the interest of making you do some figuring out yourself): You're forgetting to set j back to 1 after the inner loop.

As it is now, when j gets to be <= i once, it stays that way and the inner loop is never entered again.


Also, while it's not directly related to your question, make sure never to do j = j++ or i = i++; just do j++ and i++ (as Kshitij Mehta said in the comments). If you're interested in why, you can read this question and its answers.

Sign up to request clarification or add additional context in comments.

Comments

1

What are the rules?

while (1) { cout << "*" << '\n'; cout << "**" << '\n'; cout << "***" << '\n'; cout << "****" << '\n'; cout << "*****" << '\n'; cout << "******" << '\n'; cout << "*******" << '\n'; cout << "********" << '\n'; break; } 

Comments

1

I'll give you a hint as well: i = i++; doesn't do what you think it does.

Comments

0

I don't really know how to make it more succinct than this:

#include <iostream> #include <sstream> int main() { std::stringstream ss; int i = 10; while (i--) std::cout << (ss<<'*', ss).str() << std::endl; } 

or as for loop, cutting down a line

for(int i=10; i--;) std::cout << (ss<<'*', ss).str() << std::endl; 

If you don't mind some less efficient code:

#include <iostream> int main() { for(int i=1; i<10; std::cout << std::string(i++, '*') << std::endl); } 

2 Comments

While that's a very concise solution, it is not very legible especially for someone who's just learning C++.
Uhoh, I knew that :) But hey, it's like homework, and if I'm going to spoil the fun, I better make it interesting along the way. It worked for me that way :)
0

I can't see your triangle, but I think you need to set j to 1 before each loop on j:

while (i <= N) { i++; j = 1; while(j <= i) { cout<<"*"; j++; } cout<<endl; } 

3 Comments

When it's a homework question, try to encourage the OP to come up with the right answer by giving hints, and not the actual answer (like Seth did).
@kshitij OK, it make sense (the homework flag was added after but I could have guessed)
@teebee Glad it helped; don't forget to correct the i=i++ statements like the others said (I copy/pasted your code without paying attention).
-1
#include <iostream> using namespace std; int main() { int i, j, N=7; while(i <= N) { i++; j = 1; while(j <= i) { cout << "*"; j++; } cout << endl; } } 

1 Comment

Variable "i" is not initialized.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.