当变量等于0时,为什么我的程序在运行?

时间:2016-09-17 19:20:06

标签: c++

有人告诉我为什么我的程序在变量编号等于0时运行?如果不问任何进一步的问题或输入任何信息,它不应该停止吗?

原始程序使用了一段时间的声明....教授要求将其更改为do while语句。如果我在询问整数后输入0,那么它不应该只停在那里吗?相反,它问第二个问题。

如果声明说,在数字不为0时执行此操作,为什么在数字为0时执行此操作?

谢谢

#include <iostream>

using namespace std;

int main() {

    int number, product = 1, count = 0;

    cout << "Enter an integer number to be included in the product" << endl << "or enter 0 to end the input: ";
    cin >> number;

    do {

        product = product * number;
        count++;
        cout << "Enter an integer number to be included in the product" << "or enter 0 to end the input: ";
        cin >> number;

    } while (number != 0);

    if (count > 0) {

        cout << endl << "The product is " << product << "." << endl;
    }

}

3 个答案:

答案 0 :(得分:1)

所呈现代码的逻辑是要求输入,执行一些计算,再次询问(这次是从循环体内部),现在检查0。

因此,在程序以正常方式退出之前,您至少会得到两个问题。

或者,您可以(通常)使用 Ctrl + C 强制中止控制台程序。

程序中的逻辑被称为 loop-and-a-half ,并且可以通过从循环中间退出而无冗余地编码。

#include <iostream>
using namespace std;

int main() {
    int number, product = 1, count = 0;
    for (;;) {
        cout << "Enter an integer number to be included in the product" << "or enter 0 to end the input: ";
        cin >> number;
        if (number == 0) {
            break;
        }
        product = product * number;
        count++;
    }
    if (count > 0) {
        cout << endl << "The product is " << product << "." << endl;
    }
}

使用此修复程序,程序将在第一次输入时退出。

免责声明:编译器未触及的代码。

do ... while( number != 0 )表示,正如您在评论中解释的那样,然后是教授要求的修订后的问题,它可能如下所示:

#include <iostream>
using namespace std;

int main() {
    int number, product = 1, count = 0;
    do {
        cout << "Enter an integer number to be included in the product" << "or enter 0 to end the input: ";
        cin >> number;
        if (number != 0) {
            product = product * number;
            count++;
        }
    } while (number != 0);
    if (count > 0) {
        cout << endl << "The product is " << product << "." << endl;
    }
}

这有点尴尬,但有些人更喜欢无跳码。

同样,免责声明:编译器手中没有触及代码。

答案 1 :(得分:0)

while while表示do块将无论条件如何都至少执行一次;成功还是失败。因为这种类型的循环从执行开始,然后测试条件而不是相反。

考虑以下示例:

#include <iostream>

using namespace std;

int main() 
{

     int a = 7;

     do
     {
         cout << "Hello " << endl; // this line will be reached whatever the condition below succeeds or fails
         a--;  // this line also will be reached whatever the condition below succeeds or fails
     } while(a > 100);

    return 0;
}

在你的情况下你想在执行之前测试所以不要使用do-while循环,因为你不想“至少执行一次”所以使用while循环或者你的例子看起来像:

#include <iostream>
using namespace std;

int main() {

    int number, product = 1, count = 0;

    cout << "Enter an integer number to be included in the product" << endl << "or enter 0 to end the input: ";
    cin >> number;

    while (number != 0) 
    {

        product = product * number;
        count++;
        cout << "Enter an integer number to be included in the product" << "or enter 0 to end the input: ";
        cin >> number;

    } 

    if (count > 0) {

        cout << endl << "The product is " << product << "." << endl;
    }

    return 0;
}

答案 2 :(得分:-1)

是因为你使用了do while,试试这个。

#include <iostream>

using namespace std;

int main() {

    int number, product = 1, count = 0;

    cout << "Enter an integer number to be included in the product" << endl << "or enter 0 to end the input: ";
    cin >> number;

    while (number != 0) {

        product = product * number;
        count++;
        cout << "Enter an integer number to be included in the product" << "or enter 0 to end the input: ";
        cin >> number;

    } 

    if (count > 0) {

        cout << endl << "The product is " << product << "." << endl;
    }

}

如果你想使用do while就是这样:

#include <iostream>

using namespace std;

int main() {

    int number, product = 1, count = 0;


    do {
        cout << "Enter an integer number to be included in the product" << "or enter 0 to end the input: ";
        cin >> number;
        if(!number)
            break;
        product = product * number;
        count++;


    } while(true);

    if (count > 0) {

        cout << endl << "The product is " << product << "." << endl;
    }

}