Repeating an Action

I'm supposed to print off stars. I prompt the user for a number (we'll call it n), and then print out n lines of stars. The first line will have one star and then it adds one each line.

eg.
Enter the number of lines -> 5
*
**
***
****
*****


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

	int main() {
	    int n;
	    cout << "Enter the number of lines." << endl;
	    while (cin >> n) {
	        //--- Loop counting i up from 1 to n.
	        int i = 1;          // Initialize counter.
	        while (i <= n) {    // Test counter.
	            cout << "*";
	            i++;            // Increment counter.
	        }
	        cout << endl;
	    }
	    return 0;
	}


That gives me the entered number of stars but I don't know how to get the lines of stars up to that point.

eg. If I run it now:
Enter number of lines -> 5
*****
You will probably want to use a for loop instead, that is basically what your while loop is doing.

HINT: You will need two for loops, one inside the other for printing each line and the stars.
http://lmgtfy.com/?q=print+triangle+C%2B%2B

2nd link

Next time try google first ;)
Last edited on
Or search this site...I know that I have posted the code for this program at least once before. My approach is slightly different than most others...
@eker676 - I don't think many people would realize they can search "print triangle". Most people would be looking for stars.
Topic archived. No new replies allowed.