A C++ star pattern is a console program that prints '*' in rows. The outer for loop chooses the row. The inner loop prints each star with cout, then cout << '\n' ends that row.
'*' + 1 is an int. The char is promoted before the addition, so cout << ('*' + 1) writes 43, not another star. string("*") + "*" is the string "**". Two string literals cannot be added: "*" + "*" does not compile.
cout << '*' stays on the row.
It writes the character and does not end the line. Call cout << '\n' once after the inner loop. string(n, '*') builds a solid row of n stars. Include <string> for that constructor.
Syntax
C++ Syntax
The inner loop runs once per star. cout keeps those stars on one line, and the newline comes after that loop.
C++
for (int row = 1; row <= rows; row++) {
for (int col = 1; col <= row; col++)
cout << '*'; // same row
cout << '\n'; // end the row
}
Reference
How C++ Prints a Star
Map each part of the console shape to a C++ value or stream insertion.
Piece
C++
What it does
Star
cout << '*'
Writes the character and stays on the row
Run of stars
string(n, '*')
A string filled with n stars
Indent
string(n, ' ')
n spaces
Char +
'*' + 1
The int 43
String +
string("*") + "*"
The string "**"
New row
cout << '\n'
Ends the row
Edge only
cout << (col == 1 || col == width ? '*' : ' ')
A hollow row
Code
C++ Examples
Three programs. Each one changes only the C++ that chooses how many stars to print, and where the spaces go.
Example 1 — One star per inner-loop pass
Row 1 prints one star. Each later row prints one more, because the inner loop stops at row.
C++
#include <iostream>
using namespace std;
int main() {
int rows = 5;
for (int row = 1; row <= rows; row++) {
for (int col = 1; col <= row; col++)
cout << '*';
cout << '\n';
}
return 0;
}
Output
*
**
***
****
*****
How It Works
The outer loop walks row from 1 to 5.
cout << '*' writes the character and stays on the row.
cout << '\n' after the inner loop starts the next row.
Example 2 — Print the whole row with the string constructor
Count row down from 5. One insertion writes every star, and '\n' ends the line.
C++
#include <iostream>
#include <string>
using namespace std;
int main() {
int rows = 5;
for (int row = rows; row >= 1; row--)
cout << string(row, '*') << '\n';
return 0;
}
Output
*****
****
***
**
*
How It Works
string(row, '*') fills a std::string with that many stars.
cout writes that string, then '\n' ends the row, so the inner loop is not needed.
row-- makes each later line one star shorter.
Example 3 — Join the spaces and the stars
string(n, ' ') builds the indent. string(n, '*') builds an odd run of stars. + joins the two strings.
In C++, '*' + 1 is an int. The char is promoted before the addition, so the sum is 43 and cout writes those digits. cout << '*' writes the character. string("*") + "*" is the string "**". Two string literals cannot be added: "*" + "*" does not compile.
cout << '*' stays on the same row. cout << '\n' ends the row. Call it once after the inner loop. If the newline sits inside the inner loop, each star lands on its own line.
Yes, when the row is a solid run of stars. cout << string(row, '*') << '\n' writes that row and the newline in one statement. Include <string>. Use the inner loop when the row mixes spaces and stars, or when only the edges should be stars.
cout << string(rows - row, ' ') + string(2 * row - 1, '*') << '\n' builds the indent and the stars, then ends the row. Row 1 has one star; each later row adds two.