简体   繁体   English

无论输入如何,C ++程序都会提供相同的输出

[英]C++ program gives same output regardless of inputs

I am writing a program that takes the weight and dimensions of a package from the user. 我正在编写一个程序,该程序可以从用户那里获取包装的重量和尺寸。 If the weight is greater than or equal to 27, it should display that the package is too heavy. 如果重量大于或等于27,则应显示包装太重。 If the dimensions are greater than or equal to 0.1 meters cubed then it should display that the package is too big. 如果尺寸大于或等于0.1米(立方),则应显示包装太大。 For some reason, the code below runs fine but no matter what values I type in, it always outputs "Rejected: Too big and too heavy." 由于某些原因,下面的代码运行良好,但是无论我键入什么值,它始终输出“ Rejected:太大和太重”。 Can anyone tell me why this is happening? 谁能告诉我为什么会这样吗? I am in an introductory programming class, so I assume it is a small mistake that I've overlooked. 我在编程入门课上,所以我认为这是我忽略的一个小错误。

Thanks! 谢谢!

#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;
}

You need to initialize your variables, particularly your bool in this case 您需要初始化变量,在这种情况下尤其是bool

bool weight_passed = true;
bool dimensions_passed = true;

What is weight_passed if weight < 27 ? 如果weight < 27weight_passed是多少? And what is dimensions_passed , if dimensions < 0.1 ? 什么是dimensions_passed ,如果dimensions < 0.1 They are not assigned to--and so are indeterminate. 它们没有被分配-因此不确定。 You need to initialize them like so: 您需要像这样初始化它们:

bool weight_passed = true;
bool dimensions_passed = true;

You can do that at the declaration, or in main() . 您可以在声明中或在main()

Alternatively, you can put an else on your if s: 或者,你可以把一个else对你的if S:

if (weight >= 27)
    weight_passed = false;
else
    weight_passed = true;

if (dimensions >= 0.1)
    dimensions_passed = false;
else
    dimensions_passed = true;

Since you're new, I'll give you some other tips. 既然您是新手,我将为您提供其他提示。 The part inside the parentheses of each if can be treated like a boolean. 每个if括号内的部分可以视为布尔值。 So you can do: 因此,您可以执行以下操作:

weight_passed = (weight < 27); // equivalent to !(weight >= 27)
dimensions_passed = (dimensions < 0.1); // equivalent to !(dimensions >= 0.1)

This will not only give you the same result, but will guarantee your variables are getting assigned. 这不仅会给您相同的结果,而且将确保您的变量得到分配。

Last thing: Since what goes into if conditions is boolean logic, you don't have to use those equality operators ( == ): 最后一件事:因为什么进入if条件是布尔逻辑,你不必使用这些相等运算符( == ):

if (!dimensions_passed && !weight_passed)
    // BOTH too big AND too heavy. Geez, see a doctor man.
else if (dimensions_passed)
    // dimensions pass though, so must just be too heavy
else if (weight_passed)
    // weight_passed, so must just be too big
else
    // Goldilocks would say this one is just right.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM