谁能弄清楚为什么我的循环失败了?

Can anyone figure out why my loops are falling through?

这是我第一次 post 来这里。我是 c++ 的新手(上周刚开始),花了几个小时在这上面,但很困惑。

我知道我在这个程序中可能做错了很多事情,但我向大家保证我已经尽力了。验证输入超出了我的作业范围,但我想尝试一下,因为只是获取输入并返回它们很无聊。

基本上,输入验证适用于外循环,但对于内循环,即使无效也会失败。

#include <iostream>
using namespace std;

//Global Variables

int cubeLength = 0;
int cubeWidth = 0;
int cubeHeight = 0;
int cubeSurfaceArea = 0;
int cubeVolume = 0;
bool valid = false;

int main() {

//Ask user for cubeLength and validate input for integer values
do {
cout << "Please enter a numerical value for the length of a cube" <<endl;
cin >> cubeLength;
    if (cin.good()) {
        valid = true;
        //Ask user for cubeWidth and validate input for integer values
        do {    
            cout << "Please enter a numerical value for the width of a cube" <<endl;
            cin >> cubeWidth;
            if (cin.good()) {
                valid = true;
                //Ask user for cubeHeight and validate input for integer values
                do {
                    cout << "Please enter a numerical value for the height of a cube" <<endl;
                    cin >> cubeHeight;
                    if (cin.good()) {
                        valid = true;
                    }
                    else
                    {   
                        cin.clear();
                        cin.ignore(INT_MAX, '\n');
                        cout << "Invalid cube height. Please try again" << endl;
                    }
                }while (!valid);
            }
            else
            {   
                cin.clear();
                cin.ignore(INT_MAX, '\n');
                cout << "Invalid cube width. Please try again" << endl;
            }
        }while (!valid);
    }
    else
    {   
        cin.clear();
        cin.ignore(INT_MAX, '\n');
        cout << "Invalid cube length. Input is not an integer" << endl;
    }
} while (!valid);


//Perform calculations for surface area and volume then assign them to their associated variables
if (cubeLength >= 1 && cubeWidth >= 1 && cubeHeight >= 1)
    {
        valid = true;
        cubeSurfaceArea = ((2*(cubeWidth*cubeLength))+(2*(cubeLength*cubeHeight))+(2*(cubeWidth*cubeHeight)));
        cubeVolume = (cubeWidth*cubeLength*cubeHeight);
    }
    else {
        cout << "Sorry, one or more cube inputs is invalid. Ending program. Please restart and try again." << endl;
        return 0;
    }   

//Output surface area and volume to user
cout << "Length = " << cubeLength << " Width = " << cubeWidth << " Height = " << cubeHeight << endl;
cout << "The surface area of your cube is " << cubeSurfaceArea << "." << endl;
cout << "The volume of your cube is " << cubeVolume << "." << endl;

//Pause system and end program
return 0;
}

我在底部添加了用于计算的 if 语句,以阻止它在程序中一路下降并退出。

我还在本网站和其他网站上检查了很多关于验证整数和循环输入的类似问题,但一直无法弄清楚。我的理论是,我要么搞乱了有效的布尔逻辑,要么使用了错误的循环方法。

循环的主要问题是您将 valid 设置为 true,但从未设置为 false,因此语句 while (!valid) 永远不会计算为 false。

另一个普遍的评论是代码的布局有太多的嵌套循环。这样可以简化很多。

我没有测试下面的代码,但这种类型的结构更容易阅读 - 即分别进行每个输入,而不是将所有内容混在一起! :-)

//Ask user for cubeLength and validate input for integer values
valid = true;
do {
     cout << "Please enter a numerical value for the length of a cube" <<endl;
     cin >> cubeLength;
     if (!cin.good()) {
        valid = false;
        cin.clear();
        cin.ignore(INT_MAX, '\n');
        cout << "Invalid cube length. Input is not an integer" << endl;
    }     
} while (!valid);

//Ask user for cubeWidth and validate input for integer values
do {
    cout << "Please enter a numerical value for the width of a cube" <<endl;
    cin >> cubeWidth;
    if (!cin.good()) {
        valid = false;
        cin.clear();
        cin.ignore(INT_MAX, '\n');
        cout << "Invalid cube width. Please try again" << endl;
   }    
} while (!valid);


//Ask user for cubeHeight and validate input for integer values
do {
    cout << "Please enter a numerical value for the width of a cube" <<endl;
    cin >> cubeWidth;
    if (!cin.good()) {
        valid = false;
        cin.clear();
        cin.ignore(INT_MAX, '\n');
        cout << "Invalid cube height. Please try again" << endl;
    } 
 }while (!valid);

第一次设置后valid = true,一直到最后都是true。在再次测试之前,你应该把它带回 false

感谢所有提供帮助和反馈的人:

这个问题现在对我来说很明显,这是在输入验证失败后在循环开始时没有重置布尔值。

我根据建议重写了整个代码,现在有了一个稳定的程序!:)

#include <iostream>
using namespace std;

//Global Variables

int cubeLength = 0;
int cubeWidth = 0;
int cubeHeight = 0;
int cubeSurfaceArea = 0;
int cubeVolume = 0;
bool valid = true;
char choice;

int main() {
do {
//Ask user for cubeLength and validate input for integer values
do {
valid = true;
cout << "Please enter a numerical value for the length of a cube" <<endl;
cin >> cubeLength;
    if (cin.fail()) {
        valid = false;
        cin.clear();
        cin.ignore(INT_MAX, '\n');
        cout << "Invalid cube length. Input is not an integer" << endl;
        }
    } while (!valid);

//Ask user for cubeWidth and validate input for integer values
do {    
    valid = true;
    cout << "Please enter a numerical value for the width of a cube" <<endl;
    cin >> cubeWidth;
    if (cin.fail()) {
        valid = false;  
        cin.clear();
        cin.ignore(INT_MAX, '\n');
        cout << "Invalid cube width. Input is not an integer" << endl;
    }
} while (!valid);
//Ask user for cubeHeight and validate input for integer values
do {
    valid = true;
    cout << "Please enter a numerical value for the height of a cube" <<endl;
    cin >> cubeHeight;
    if (cin.fail()) {   
        valid = false;
        cin.clear();
        cin.ignore(INT_MAX, '\n');
        cout << "Invalid cube height. Input is not an integer" << endl;
    }
}while (!valid);

//Perform calculations for surface area and volume then assign them to their associated variables
if (cubeLength >= 1 && cubeWidth >= 1 && cubeHeight >= 1)
    {
        cubeSurfaceArea = ((2*(cubeWidth*cubeLength))+(2*(cubeLength*cubeHeight))+(2*(cubeWidth*cubeHeight)));
        cubeVolume = (cubeWidth*cubeLength*cubeHeight);
    }
    else {
        cout << "Sorry, one or more cube inputs is invalid. Ending program. Please restart and try again." << endl;
        return 0;
    }   

//Output surface area and volume to user
cout << "Length = " << cubeLength << " Width = " << cubeWidth << " Height = " << cubeHeight << endl;
cout << "The surface area of your cube is " << cubeSurfaceArea << "." << endl;
cout << "The volume of your cube is " << cubeVolume << "." << endl;
cout << "Would you like to try again? (y/n)" << endl;
cin >> choice;

} while (choice != 'n');

//Pause system and end program
return 0;
}