尽管条件未满足,循环仍结束

时间:2019-03-31 11:58:02

标签: c++

我正在尝试编写一个不断询问输入的代码,直到两个连续数字的和为三位数,即正数和偶数为止。例如(输入为粗体字)

输入? 23 输入? -21 输入? 120 输入? -2

23 + −21 = 2不是3位数字; −21 + 120 = 99不是3位数字,也不是偶数。然后,120 + -2 = 118是3位数,正数和偶数。因此,程序停止。

但是,我编写了这样的代码,在键入前两个数字后,代码虽然不是三位数字,正数或偶数,但还是结束了。我做错了什么?

using namespace std;
int main() {
    cout << "Input? ";
    int a, b, c, sum;
    cin >> a;
    cout << "Input? ";
    cin >> b;
    sum = a+b;
    while (sum>=0) {
        cout << "Input? ";
        cin >> c;
        a=b;
        b=c;
    }
    while (sum%2==1) {
        cout << "Input? ";
        cin >> c;
        a=b;
        b=c;
    }
    while (sum<100 && sum>999) {
        cout << "Input? ";
        cin >> c;
        a=b;
        b=c;
    }
    return 0;
}

Nvm,我忘了更改总和。但是,仍然存在问题。该代码永无止境,因为它一直在要求我输入。我的新代码:

using namespace std;
int main() {
    int a, b, c, sum;
    cout << "Input? ";
    cin >> a;
    cout << "Input? ";
    cin >> b;
    sum = a+b;
    while (sum>=0) {
        cout << "Input? ";
        cin >> c;
        a=b;
        b=c;
        sum = a+b;
    }
    while (sum%2==1) {
        cout << "Input? ";
        cin >> c;
        a=b;
        b=c;
        sum = a+b;
    }
    while (sum<100) {
        cout << "Input? ";
        cin >> c;
        a=b;
        b=c;
        sum = a+b;
    }
    while (sum<999) {
        cout << "Input? ";
        cin >> c;
        a=b;
        b=c;
        sum = a+b;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我真的相信stackoverflow的教学和教育目的:它是有史以来最好的老师之一!

为了让您理解可以将printf(或您的情况下的cout)用作更简单的调试方法,在这里我发布了一个简单的代码版本,其中包含了一些注释。线

cout << "the input is " << a << endl; //you can use this line everywhere as primitive debug method
可以在代码的每一点插入

来打印关键信息。我真的想敦促您探索调试的世界(例如,使用GDB)。有一百种不同的方法。

但是,完成该工作的代码版本为:

#include <iostream>

using namespace std;

int main() {
    int previous_number, current_number;
    // run this loop forever until two consecutive input satisfy the conditions
    cout << "Input? ";
    cin >> previous_number;
    //cout << "the input is " << a << endl; //you can use this line everywhere as primitive debug method

    while(1){ //run this loop forever until the conditions are satisfied
        cout << "Input? ";
        cin >> current_number;
        if( (previous_number + current_number) < 0 ){
            cout << "the sum of the numbers is negative:'" << (previous_number + current_number) <<"'. Insert another input " << endl;
            previous_number=current_number;
        } else if ( (previous_number + current_number) < 100 || (previous_number + current_number) > 999 ){
            cout << "the sum of the numbers is not a three-digit number:'" << (previous_number + current_number) <<"'. Insert another input " << endl;
            previous_number=current_number;
        } else if ( (previous_number + current_number) % 2 == 1 ){
            cout << "the sum of the numbers is odd:'" << (previous_number + current_number) <<"'. Insert another input " << endl;
            previous_number=current_number;
        } else {
            cout << "the sum of the numbers is odd, positive and three-digit:'" << (previous_number + current_number) <<"'. " << endl;
            break;
        }

    }
    return 0;
    cout << "------The END------" << endl;
}

使用输入时,输出为:

Input? 23
Input? -21
the sum of the numbers is not a three-digit number:'2'. Insert another input 
Input? 120
the sum of the numbers is not a three-digit number:'99'. Insert another input 
Input? -2
the sum of the numbers is odd, positive and three-digit:'118'. 
  

请注意,代码中有很多cout。原因是发现可能的代码错误。这是更简单的方法之一   调试您的代码。