简体   繁体   English

如何找出为什么我的程序一直有一个无限循环?

[英]How to find out why my program keeps having an infinite loop?

I can't seem to find what's wrong with my code, I'm trying to end the loop once the answers is equals to 0 but it keeps going on an infinite loop.我似乎找不到我的代码有什么问题,一旦答案等于 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);  
}

There are, essentially, two problems in your code, both of which, in themselves, will make your loop run endlessly.本质上,您的代码中存在两个问题,这两个问题本身都会使您的循环无休止地运行。

Starting from the outside and working inwards: The test at the end of your (outer) while loop will always be "true", as you have while (x >= 0) ;从外部开始,向内工作:(外部) while循环结束时的测试将始终为“真”,就像您所拥有的那样while (x >= 0) so, even when x gets to zero (as it will), the loop will keep running, (And, once x is zero it will remain zero!)因此,即使 x 变为零(因为它会),循环将继续运行,(并且,一旦 x 为零,它将保持为零!)

Second, the two 'inner' while loops shouldn't be loops at all!其次,两个“内部”while 循环根本不应该是循环! You want one or the other 'block' to run once only for each main loop - so use an if... else structure.您希望一个或另一个“块”只为每个主循环运行一次- 所以使用if... else结构。

The following is a corrected version of your code:以下是您的代码的更正版本:

#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;
}

There are a few other things that could be changed to make the code more "efficient," but I'll let you peruse the MNC (Minimum Necessary Change) before we start editing towards a BPC (Best Possible Code)!还有一些其他的事情可以更改以使代码更“高效”,但在我们开始编辑 BPC(最佳可能代码)之前,我会让您仔细阅读 MNC(最小必要更改)!

EDIT: OK, due to 'peer pressure' from comments, I'll put in a suggested BPC now:编辑:好的,由于评论中的“同行压力”,我现在将提出建议的 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 discard 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM