我正在编写一个程序,该程序可以从用户那里获取包装的重量和尺寸。如果重量大于或等于27,则应显示包装太重。如果尺寸大于或等于0.1米(立方),则应显示包装太大。由于某种原因,下面的代码运行良好,但是无论我输入什么值,它始终输出“ Rejected:太大和太重”。谁能告诉我为什么会这样吗?我在编程入门课上,所以我认为这是我忽略的一个小错误。

谢谢!

#include <iostream>

using namespace std;

double weight;
double length;
double width;
double height;
double dimensions;
bool weight_passed;
bool dimensions_passed;

int main()
{
    cout << "Enter the weight of the package in kilograms: ";
    cin >> weight;
    cout << "Enter the length of the package in meters: ";
    cin >> length;
    cout << "Enter the width of the package in meters: ";
    cin >> width;
    cout << "Enter the height of the package in meters: ";
    cin >> height;
    dimensions = length * width * height;
    if (weight >= 27)
        weight_passed = false;
    if (dimensions >= 0.1)
        dimensions_passed = false;

    if (dimensions_passed == false && weight_passed == false)
        cout << "Rejected: Too big and too heavy." << endl;
    else if (dimensions_passed == true && weight_passed == false)
        cout << "Rejected: Too heavy." << endl;
    else if (dimensions_passed == false && weight_passed == true)
        cout << "Rejected: Too big." << endl;
    else
        cout << "Accepted!" << endl;
    return 0;
}

最佳答案

您需要初始化变量,在这种情况下尤其是bool

bool weight_passed = true;
bool dimensions_passed = true;

关于c++ - 无论输入如何,C++程序都会提供相同的输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25755357/

10-10 22:17
查看更多