根据用户输入的整数显示字符

时间:2018-10-29 00:49:51

标签: c++

我确信这对这里的许多人来说似乎是新手,但是我目前正在尝试学习C ++,并且对将我的知识应用于现实世界有深入的了解。

问题描述: 编写一个程序,使用星号图形化地描述整数的大小,并创建一个 直方图提示:内循环处理星号的打印,外循环处理 直到输入零为止。确保您不接受负数。 样品运行:

Enter a positive integer (0 to quit): 5
*****
Enter a positive integer (0 to quit): 8
********
Enter a positive integer (0 to quit): -5
Enter a positive integer (0 to quit): -10
Enter a positive integer (0 to quit): 15
***************
Enter a positive integer (0 to quit): 0
Good Bye!

我的当前代码(我知道这绝对不对,但是有助于证明我的想法,我知道您需要使用嵌套循环才能正确回答此问题。):

#include <iostream>
using namespace std;
int main()
{
    int ast; //Number of asterisk wanted to be displayed
    char asts='*'; //Actual display coressponding to asterisk
    cout<<"Enter a positive integar, (0 to quit)"<<endl;
    cin>>ast;
    while(ast!=0)
    {
        if(ast>0) // Ensuring no negative integars are entered
        {
        asts=ast;
        cout<<asts<<endl;
        cout<<endl;
        }
        else //Display if negative or other invalid data is entered
        cout<<"Invalid Data, negative values are not accepted, try a positive integar or 0 to quit"<<endl;
        cout<<"Do you want to continue? If so, enter another integar (0 to quit)"<<endl;
        cin>>ast;
    }
    cout<<"Thank you for using the program."<<endl;
    return(0);
}

谢谢您的帮助! -科林

3 个答案:

答案 0 :(得分:1)

基本思想是(带有伪代码):

get number of asterisks
while number is not zero:
    if number is negative:
        output error message
    else:
        do number times:
            output *
        output newline
    get number of asterisks

您的嵌套循环有while number is not zerodo number times


但是,作为开发人员,您应该学习的最早技能之一是将任务正确分配给特定的代码段(方法,函数,库等)。特别是,最好对代码进行模块化,以使每个部分都有明确的用途,然后在这些部分中构建“上”层。

为此,是我要解决的问题。首先,定义在这种情况下有用的功能。

getAsterCount()开始,它负责询​​问用户他们想要多少个星号,并验证它是否有效(一遍又一遍地询问,直到是正确的)。例如:

#include <iostream>

int getAsterCount() {
    // Until we get valid response.

    for(;;) {
        int count;

        // Get value, checking for error, forcing exit if so.

        std::cout << "Enter the number of asterisks, zero to exit: ";
        if (! (std::cin >> count)) {
            std::cout << "*** ERROR: could not read number\n";
            return 0;
        }

        // Any non-negative value is allaowed.

        if (count >= 0) {
            return count;
        }

        std::cout << "That was less than zero, try again.\n";
    }
}

一旦安装到位,您就不必再为用户为该程序提供无效信息而担心,因为该函数将捕获该信息(并且您可以从任何地方调用它,如下面的main()函数所示)

接下来,根据您现在拥有的计数,提供一个实际上将输出星号的功能:

void outputAster(int count) {
    // Simple loop for asterisks then new line.

    for (int i = 0; i < count; ++i) {
        std::cout << '*';
    }
    std::cout << '\n';
}

有了这些,您的主代码在概念上将变得更加简单:

int main() {
    int count = getAsterCount();
    while (count > 0) {
        outputAster(count);
        count = getAsterCount();
    }
    std::cout << "Thank you for using the program. Now get off my lawn.\n";
}

答案 1 :(得分:0)

现在您正在尝试做:

asts=ast;

没有什么意义,因为这只会将输入的整数分配给asts

要打印*中的幅值,您需要从0到输入的整数循环并每次输出一个星号:

for(int i = 0; i < ast; i++) {
   std::cout << "*";
}
std::cout << "\n";

输出:

Enter a positive integar, (0 to quit)
5
*****
Do you want to continue? If so, enter another integar (0 to quit)
3
***
Do you want to continue? If so, enter another integar (0 to quit)
5
*****
Do you want to continue? If so, enter another integar (0 to quit)
0

答案 2 :(得分:0)

我喜欢有点不同的方法,没有循环。

char asts[] = “*********\n”;

在检查输入值在范围内之后,只需写出字符串的结尾:

std::cout << (asts + 9 - ast);