Code Should Explain It self..

#include <iostream>

using namespace std;

int main()
{
    unsigned int digit;
    
    for( digit = 0; digit < 10; digit++ )
    {
         /*
         
          I WANT THE OUTPUT TO CARRY ON UNTIL DIGIT == 10 & OUTPUT
         
         *
         **
         ***
         ****
         *****
         ******
         *******
         ********
         *********
         ********** 
         
         */
    }
    
    cin.get();
    
    return 0;
}

Dani AI

Generated

A few quick, practical alternatives and notes that build on what , and already pointed out.

A compact way to produce each line in one call is to use the std::string constructor that repeats a character. This keeps the loop body tiny and makes the intent obvious:

#include <iostream>
#include <string>

int main() {
    const int n = 10;
    for (int i = 1; i <= n; ++i)
        std::cout << std::string(i, '*') << '\n';
}

std::string(count, ch) constructs a string of count copies of ch — see the reference for details. (If you prefer not to construct a fresh string each iteration, the next snippet shows a simple reuse technique.)

To avoid reconstructing strings repeatedly and to reduce reallocations, build one string and append a star each loop:

std::string line;
line.reserve(n);           // optional: avoid reallocations
for (int i = 0; i < n; ++i) {
    line.push_back('*');
    std::cout << line << '\n';
}

A few extra tips: be clear about loop bounds (use i = 1..<= n if you want 1..10 stars). Prefer '\n' over std::endl to avoid an extra flush on every line (see the i/o manipulator docs). Use an appropriate integer type (int or size_t) rather than unsigned unless you need it, to avoid underflow surprises. For very large triangles the total output size is O(n^2), so printing will be the bottleneck regardless of approach.

References: std::string constructors and modifiers on cppreference:

Recommended Answers

All 8 Replies

Put another loop inside that loop. You want to print (digit+1) asterisks and then a newline.

When you say "digit+1" could i use a switch case with case "1" then print 1 etc etc?

Um, huh? I mean this, basically:

for ( int i = 0; i < digit + 1; i++ )
  std::cout<<'*';
std::cout<<'\n';

I meant something like

switch (digit)
{
       case 1: cout << "*" << endl; break;
       case 2: cout << "**" << endl; break;
}

But thanks anyways

Essentially you are extending the triangular number sequence: 1, 3, 6, 10, 15, ... (i.e. each number is the number of asterisks printed so far). Here is the pattern:
http://www.mathematische-basteleien.de/triangularnumber.htm

Using a switch statement would not be very effective if you needed, say, 100 lines (heck even having to make 10 separate cases scares me...if a problem is not fun, then there is a better way to solve it, and writing out 10 switch statements does not sound like fun :)).

In any case, notice that on each successive line, there is 1 more asterisk than on the previous line (this pattern will become more evident if you follow the link I gave you above). If you use a nested loop as Narue suggested (i.e. another for-loop within your existing for-loop), you can print out a number of asterisks equal to the current value of "digit" plus 1.

That is, if digit = 0, your inner for loop will run from 0 to 0, and output 1 asterisk.

If digit = 1, the inner loop would run from 0 to 1, and output 2 asterisks.

If digit = 5, the inner loop would run from 0, 1 , ... , 5, and would output 6 asterisks...

EDIT: and Narue posted again before me...so this kinda becomes redundant

>I meant something like
That works, until you want a bigger triangle:

for( digit = 0; digit < 20; digit++ ) {
  // Uh-oh. Now you need to add 10 more cases
}

>But thanks anyways
For future reference, I despise any variation of "thanks anyway". It has extremely rude connotations.

Ohh I'm so sorry I didn't mean to be rude but thankful is "Thank You" ok?

Try this:

#include <iostream>

using namespace std;

int main()
{
	unsigned int digit1;
	unsigned int digit2;

	for( digit1 = 0; digit1 < 10; digit1++ ) {
		for( digit2 = 0; digit2 < digit1 + 1; digit2++ ) {
			cout << '*';
		}
		cout << '\n';
	}

	cin.get();

	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.