首页 > 解决方案 > 谁能弄清楚为什么我的循环会失败?

问题描述

这是我在此的头一篇博文。我是 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 语句,以阻止它在整个程序中一直下降并退出。

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

标签: c++loopsvalidationintcin

解决方案


循环的主要问题是您设置validtrue但从不设置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);

推荐阅读