使用while循环来计算和显示数字

时间:2015-03-23 12:20:32

标签: c++ visual-studio visual-studio-2013

我坚持的问题要求用户输入“基数5” (一个只包含0s,1s,2s,3s或/和4s的数字)

我必须测试一下它是否实际上是一个基数为5的数字,如果是这样,我必须输出每个数字的出现次数(0的数量,1的数量等)

我已经在网上寻求建议,但我发现的大部分内容都使用了课程资料中没有涉及的内容,而且我理解并掌握课程相关资料对我来说非常重要。

例如,我知道大多数人会为此使用“数组”,但在这种情况下我不能这样做。

因此,为了明确我正在寻找的内容,是能够使用WHILE循环执行此问题所需的内容。

这是我的一次尝试:

#include <iostream>
using namespace std;

int main()
{
    int zeros = 0;
    int ones = 0;
    int twos = 0;
    int threes = 0;
    int fours = 0;

    int number;


    cout << "Please enter a base 5 number\n";
    cin >> number;
    cout << endl;

    while (cin.get() == '0')
    {
        zeros++;
    }

    while (cin.get() == '1')
    {
        ones++;
    }

    while (cin.get() == '2')
    {
        twos++;
    }

    while (cin.get() == '3')
    {
        threes++;
    }

    while (cin.get() == '4')
    {
        fours++;
    }

    cout << "Number of Zeros: " << zeros;
    cout << endl;
    cout << "Number of Ones: " << ones;
    cout << endl;
    cout << "Number of Twos: " << twos;
    cout << endl;
    cout << "Number of Threes: " << threes;
    cout << endl;
    cout << "Number of Fours: " << fours;
    cout << endl;


    return 0;
}

我已尝试使用和不使用单引号围绕数字,以及代码的许多其他微小变化无济于事。 另外我相信我错过了一个“if”语句,在使用“whiles”之前测试它是否实际上是一个基数为5的数字,但我只是想不出一种方法来测试

也许是这样的:

while (cin.get() != '0' || '1' || '2' || '3' || '4')

...

无论哪种方式,“whiles”在这种情况下都不算数,我无法弄明白为什么!

此外,我选择尝试此方法的原因(除了要求使用while循环和其他相关的课程相关主题),我在之前的程序中使用相同的方法,完美地工作。前一个基本上是用户输入字符串,我必须计算并输出其中的字符数(包括空格,符号等),直到用户点击“返回”。这是旧代码的一部分:

char input;
int counter = 0;

while (cin.get() != '\n')
    {
        counter++;        
    }

tl; dr输入基数为5,计数+输出每个数字的出现次数,必须使用while循环

1 个答案:

答案 0 :(得分:0)

您接受输入为int,然后检查char。如果您确实想要检查每个char,则应接受string,然后使用for循环迭代每个字母。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int zeros = 0;
    int ones = 0;
    int twos = 0;
    int threes = 0;
    int fours = 0;

    string number;


    cout << "Please enter a base 5 number\n";
    cin >> number;
    cout << endl;

    for (char digit : number)
    {
        if (digit == '0')
        {
            zeros++;
        }
        else if (digit == '1')
        {
            ones++;
        }
        else if (digit == '2')
        {
            twos++;
        }
        else if (digit == '3')
        {
            threes++;
        }
        else if (digit == '4')
        {
            fours++;
        }
    }

    cout << "Number of Zeros: " << zeros;
    cout << endl;
    cout << "Number of Ones: " << ones;
    cout << endl;
    cout << "Number of Twos: " << twos;
    cout << endl;
    cout << "Number of Threes: " << threes;
    cout << endl;
    cout << "Number of Fours: " << fours;
    cout << endl;
    return 0;
}

Live Demo

也可以不使用一堆变量作为计数器,而只需使用数组

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int counts[5] = {0, 0, 0, 0, 0};   
    string number;   

    cout << "Please enter a base 5 number\n";
    cin >> number;
    cout << endl;

    for (char digit : number)
    {
        if (digit == '0')
        {
            counts[0]++;
        }
        else if (digit == '1')
        {
            counts[1]++;
        }
        else if (digit == '2')
        {
            counts[2]++;
        }
        else if (digit == '3')
        {
            counts[3]++;
        }
        else if (digit == '4')
        {
            counts[4]++;
        }
    }

    cout << "Number of Zeros: " << counts[0];
    cout << endl;
    cout << "Number of Ones: " << counts[1];
    cout << endl;
    cout << "Number of Twos: " << counts[2];
    cout << endl;
    cout << "Number of Threes: " << counts[3];
    cout << endl;
    cout << "Number of Fours: " << counts[4];
    cout << endl;
    return 0;
}