如何用星号制作一系列越来越大的字母?

时间:2017-10-08 21:25:52

标签: c++

需要使用Code :: Blocks在C ++中创建包含三个函数的菜单的程序:

1。退出。

2. 将数字增加1(例如:1,2,3,4,5)直到用户指定的数字。

3。一系列用星号制作的L',两侧逐渐逐渐增长。

Example of what the function is supposed to print

Example of what the function in my program prints

注意:我刚刚开始编码,所以如果有任何解决方案是使用简单的for,'s,而是等等,我会更喜欢。谢谢。

这是我现在的代码,功能1和2工作,但带星号的功能3显然没有:

#include <iostream>

using namespace std;

//This function prints a number starting from 1 and then adds 1.
//Example: 12345.
int Numbers()
{
    int num,n;

    cout << "Insert a number: " <<endl;
    cin >> num;

    for ( n=1; n<=num; n++)
    {
        cout << n <<endl;
    }
    cout <<endl;
}
//This function prints a series of L's made with asterisks.
//Example:
//*
//*
//***

int Asterisks()
{
    int ast,a,s;


    cout << "Insert a number higher than 3: " <<endl;
    cin >> ast;

    for ( s=3; s<ast; s++)
    {
        for ( a=0; a<s; ++a)
        {
            cout << "* " <<endl;
        }
        for ( a=0; a<s; a++)
        {
            cout << "* ";
        }
    }
    cout <<endl;
}

int main()
{
    int x=0;
    while (x!=1)
    {
        cout << "1. Exit" <<endl;
        cout << "2. Numbers" <<endl;
        cout << "3. Asterisks" <<endl;
        cin >> x;

        if (x==2)
        {
            Numbers();
        }

        if (x==3)
        {
            Asterisks();
        }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

从第二个“L”开始,您的代码将第一个星号附加到前一个“L”的最后一个。放置cout << endl,使其在每个'L'后打印一个新行:

for ( s=3; s<ast; s++)
{
    for ( a=0; a<s; ++a)
    {
        cout << "* " <<endl;
    }
    for ( a=0; a<s; a++)
    {
        cout << "* ";
    }
    cout <<endl;
}