如何找出为什么我的程序不断出现无限循环?

时间:2019-10-10 05:25:20

标签: c++ visual-c++

我似乎找不到我的代码出了什么问题,我试图在答案等于0时结束循环,但它一直在无限循环。

#include <iostream>
int main() {
    using namespace std;
    int x, remainder;
    cout << "please enter a positive integer number: " << endl;
    string tab;
    tab = '\t';
    cin >> x;
    remainder = x % 2;

    do{
        while ( x % 2 != 0)
        {
            cout << x << " is odd" << tab << "Subtract 1" << tab << " Half of " << x - 1 << " is " << x / 2;
            x = (x - 1) / 2;
            cout  << endl;

        }

        while (x % 2 == 0)
        {
            cout << x << " is even" << tab << "Subtract 0" << tab << "Half of  " << x << " is " << x / 2;
            x = x / 2;
            cout << endl;
        }

    }
    while (x >= 0);  
}

1 个答案:

答案 0 :(得分:2)

从根本上讲,您的代码中存在两个问题,而这两个问题本身都会使您的循环不断地运行。

从外部开始,向内进行:while(外部)循环结束时的测试将始终为“ true”,因为您有while (x >= 0);因此,即使x变为零(将如此),循环也将继续运行! (而且,一旦x为零,它将保持为零!)

第二,两个“内部” while循环根本不应该是循环!您希望每个主循环中的一个或另一个“块”仅运行一次 -因此请使用if ... else结构。

以下是您的代码的更正版本:

#include <iostream>

int main() {
//  using namespace std; // Generally, not good practice (and frowned-upon here on SO)
    using std::cin;  using std::cout; using std::endl; // Use only those you want to!
    using std::string;
    int x, remainder;
    cout << "please enter a positive integer number: " << endl;
    string tab;
    tab = '\t';
    cin >> x;
    remainder = x % 2;

    do {
        if (x % 2 != 0)
        {
            cout << x << " is odd" << tab << "Subtract 1" << tab << " Half of " << x - 1 << " is " << x / 2;
            x = (x - 1) / 2;
            cout << endl;
        }
        else // if (x % 2 == 0) ... but we don't need to test this again.
        {
            cout << x << " is even" << tab << "Subtract 0" << tab << "Half of  " << x << " is " << x / 2;
            x = x / 2;
            cout << endl;
        }
    } while (x > 0); // You run forever if you have x >= 0!
    return 0;
}

还有一些其他事情可以更改,以使代码更“高效”,但是在我们开始对BPC(最佳可能代码)进行编辑之前,我将让您细读MNC(最小必要更改) )!

编辑:好的,由于评论'的'同辈压力',我现在放入一个 建议 BPC:

#include <iostream>

int main() {
    using std::cin;  using std::cout; using std::endl; // Use only those you want to!
    int x;// , remainder; // "remainder" is never used, so we can scrap it!
    cout << "Please enter a positive integer number: " << endl;
    cin >> x; // Not critical, but I like to put such a cin right after the prompting cout.
    std::string tab{ "\t" }; // Let's initialise more directly!
    do {
        // As there is only one line (now) inside each if/else block, we can leave out the {} ...
        if (x % 2 != 0)
            cout << x << " is odd" << tab << "Subtract 1" << tab << "Half of " << x - 1 << " is " << x / 2;
        else
            cout << x << " is even" << tab << "Subtract 0" << tab << "Half of  " << x << " is " << x / 2;
        // We can put the following two line outside the tests, as they will be the same in both cases:
        x = x / 2; // When x is ODD, this will give the SAME answer as x = (x - 1)/2 (as you noticed in your first cout)
        cout << endl;
    } while (x > 0); // You run forever if you have x >= 0!
    return 0;
}