c++ - 谁能弄清楚为什么我的循环会失败?
问题描述
这是我在此的头一篇博文。我是 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)
永远不会评估为假。
另一个普遍的评论是代码的布局有太多的嵌套循环。这可以简化很多。
我没有测试下面的代码,但是这种类型的结构更容易阅读 - 即分别进行每个输入,而不是将所有输入混在一起!:-)
//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);
推荐阅读
- reactjs - 如何新建 reactjs 和 react-native 项目并整理源码仓库?
- api - 如何在 Kotlin 的 forEach 循环中使用协程?
- javascript - 一次通话向所有订阅者发送网络推送通知
- c# - 使用 Vue JS 和 .Net Core Web API 之类的堆栈 - 我在哪里实现身份验证 (Azure AD)?
- arrays - 从 JSON 响应中的逗号分隔字符串中获取数据,并根据 IONIC 3 中的值禁用或启用按钮
- python-3.x - 如何使用菜单中没有“文件->退出”的pywinauto关闭窗口?
- javascript - 如何为 Webpack 4 中的每个输出导出 contenthash 值?
- clang - Xcode 11 ld 错误“您的二进制文件不是 /usr/lib/libcrypto.dylib 的允许客户端”
- javascript - 匹配一半在标签内一半在外面的字符串
- javascript - *ngFor 在获取新数据之前正在更新