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

Can anyone figure out why my loops are falling through?

本文关键字:失败 循环 我的 弄清楚 为什么      更新时间:2023-10-16

这是我在这里的第一篇文章。我是 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;
}