打印没有嵌套条件的三角形

Moh*_*med 5 c++ loops

#include<iostream>

using namespace std;
int main() {
    int height, star{ 0 };
    cout << "Enter height of triangle";
    cin >> height;
    for(int i=1; i<=height; i++)
    {
        if(star<i)
        {
            cout << "*";
            ++star;
            continue;
        }
        cout << endl;
        star = 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是在一行中打印星星,我想在第一行打印一颗星星,然后在第二行打印 2 颗星,依此类推。

例子:

*
**
***
****
Run Code Online (Sandbox Code Playgroud)

图片:

在此处输入图片说明

Ice*_*ind 4

您应该能够简单地执行以下操作:

#include <iostream>

using namespace std;

int main()
{
    int height;
    cout << "Enter height of triangle";
    cin >> height;
    for(int i=1; i<=height; i++)
    {
        cout << string(i, '*') << endl;            
    }
}
Run Code Online (Sandbox Code Playgroud)