使用For循环时出错

时间:2015-02-08 05:01:09

标签: c++

我在运行循环时遇到问题。 我必须运行一个程序,用户输入4位数字,它将显示出现多少零。该程序将运行取决于用户希望它运行的次数。所以在开始时我提示用户输入程序运行的次数,然后实际的程序将在For循环中运行。但到目前为止,我只能运行一次。有人能帮我吗?谢谢。这是我的代码

#include <iostream>

using namespace std;

int main()
{
    int numberTimes;

    cout << "How many times do you want to run this check?\n";
    cin >> numberTimes;

    for (int counter = 0; counter < numberTimes; counter++);
    {
        int positiveInteger;

        //prompt user to enter a positive integer
        cout << "Please enter a positive integer value.\n";
        cin >> positiveInteger;


        //conditional statement
        while((positiveInteger <=0) || (positiveInteger > 9999))
        {
            cout << "Invalid Value!!! Please try again.\n";
            cout << "Please enter a positive integer value.\n";
            cin >> positiveInteger; 
        }

        cout << "Processing the value " << positiveInteger << ".\n";

        int zeroCount = 0;
        int firstNumber = positiveInteger%10; //separate the first number 
        if (firstNumber == 0) //determine if the first digit is zero
        {
            zeroCount = zeroCount + 1;
        }

        int digitOne = positiveInteger/10; //omitted first digit number
        int secondNumber = digitOne%10; //separate the second number
        if (secondNumber == 0) //determine if the second digit is zero
        {
            zeroCount = zeroCount + 1;
        }

        int digitTwo = digitOne/10; //omitted the second number
        int thirdNumber = digitTwo%10; //separate the third number
        if (thirdNumber == 0) //determine if the third digit is zero
        {
            zeroCount = zeroCount + 1;
        }

        int digitThree = digitTwo/10; //omitted the third number
        int fourthNumber = digitThree%10; //separate the fourth number
        if (fourthNumber == 0) //determine if the fourth digit is zero
        {
            zeroCount = zeroCount + 1;
        }

        cout << "Your first digit number is " << firstNumber << ".\n";
        cout << "Your second digit number is " << secondNumber << ".\n";
        cout << "Your third digit number is " << thirdNumber << ".\n";
        cout << "Your fourth digit number is " << fourthNumber << ".\n";

        cout << "Number of zero appear in your integer is " << zeroCount << ".\n";

        if (zeroCount % 2 == 0) //determine if the number is even or odd
        {
        cout << "Your number zero appear even times.\n";
        } else 
        {
            cout << "Your number zero appear odd times.\n";
        }
    }

    cout << "You have run this program " << numberTimes << ".\n";
    cout << "Thank you and good bye.";

    return 0;

}

2 个答案:

答案 0 :(得分:0)

您在for语句的末尾添加了分号。这样:

for (int counter = 0; counter < numberTimes; counter++);

应该是这样的:

for (int counter = 0; counter < numberTimes; counter++)

答案 1 :(得分:0)

问题是在;行末尾使用for

for (int counter = 0; counter < numberTimes; counter++);

在您使用的这种情况下,它会迭代循环但不执行任何操作。

换句话说,它现在等于以下程序:

for (int counter = 0; counter < numberTimes; counter++)
   {
   }

您也可以使用此程序:(添加互补线)

#include <iostream>

using namespace std;

int main()
{
    int positiveInteger;
    int numberTimes;
    int digit;

    cout << "How many times do you want to run this check?\n";
    cin >> numberTimes;

    while(numberTimes)
    {
        int zeroRepitation=0;

        cout << "Please enter a positive integer value.\n";
        cin >> positiveInteger;

        if ((positiveInteger <=0) || (positiveInteger > 9999))
        {
            cout << "Invalid Value!!! Try again!\n";
            continue;
        }

        for (int i=1;i<5;i++)
        {
            digit = positiveInteger % 10;
            if (digit=0)
                {
                ++zeroRepitation;
                }
            positiveInteger = positiveInteger / 10;
        }


        cout<<"Number of zeros in this number= "<<zeroRepitation;       
        --numberTimes;
    }