C ++中简单If语句实践中的逻辑错误

时间:2017-03-16 03:14:42

标签: c++ if-statement logic conditional

我不确定为什么CHARGE_1被评估为.80,无论我输入的数量是1还是19,其他if语句都是如此。我继续得到奇怪的数字,如.80和.64作为输出。请帮我弄清楚这有什么问题。

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    const int MONTHLY_FEE = 10;
    const double CHARGE_1 = .10;
    const double CHARGE_2 = .08;
    const double CHARGE_3 = .06;
    const double CHARGE_4 = .04;
            int numChecks;
          double charge1Total = numChecks * CHARGE_1;
          double charge2Total = numChecks * CHARGE_2;
          double charge3Total = numChecks * CHARGE_3;
          double charge4Total = numChecks * CHARGE_4;

    cout << "Please enter how many checks you have used.\n";
    cout << "and I will tell you how much your charge is. ";
    cin >> numChecks;

    if((numChecks > 0) && (numChecks < 20))
    {
        cout << "Your total is $" << fixed << setprecision(2) <<    
        (charge1Total + MONTHLY_FEE) << ".";
    }
    else if ((numChecks >= 20) && (numChecks <= 39))
    {
        cout << "Your total is $" << fixed << setprecision(2) <<    
       (charge2Total + MONTHLY_FEE) << ".";
    }
    else if ((numChecks >= 40) && (numChecks <= 59))
    {
        cout << "Your total is $" << fixed << setprecision(2) <<    
        (charge3Total + MONTHLY_FEE) << ".";
    }
    else if (numChecks >=60)
    {
        cout << "Your total is $" << fixed << setprecision(2) << 
        (charge4Total + MONTHLY_FEE) << ".";
    }
}

3 个答案:

答案 0 :(得分:0)

您需要在使用numChecks之前初始化它的值。有四个地方你使用未初始化的变量,这意味着值可以是任何值。

在行

  double charge1Total = numChecks * CHARGE_1;
  double charge2Total = numChecks * CHARGE_2;
  double charge3Total = numChecks * CHARGE_3;
  double charge4Total = numChecks * CHARGE_4;

numChecks可以是任何东西,所以把这些行放在你

所在的语句之下
cin >> numChecks;

答案 1 :(得分:0)

chargeXTotal变量在编译时进行评估,因为您在变量声明中将其公式定义为:

double charge1Total = numChecks * CHARGE_1;

由于numChecks是未初始化的var:

int numChecks;

因此chargeXTotal每个都已设置了一个数字,而不是cin所期望的数字,所以正确的方法是在{{1}之后保持chargeXTotal的声明}}:

cin

这将使编译器在用户输入之后计算cin >> numChecks; double charge1Total = numChecks * CHARGE_1; 的正确值,而不是在编译时使用未初始化的var。

答案 2 :(得分:0)

代码中存在一个简单的逻辑错误:在初始化之前,您使用numChecks执行数学运算。使用它执行计算后,您接受numChecks的输入:

int numChecks; // numChecks is uninitialized here; it is only defined as an int variable

// in the following 4 calculations, you multiply CHARGE_N with an uninitialized variable
double charge1Total = numChecks * CHARGE_1;
double charge2Total = numChecks * CHARGE_2;
double charge3Total = numChecks * CHARGE_3;
double charge4Total = numChecks * CHARGE_4;

// here you accept the input of numChecks after you have oerformed the calculations with it
cout << "Please enter how many checks you have used.\n";
cout << "and I will tell you how much your charge is. ";
cin >> numChecks;

由于在执行数学运算之前未初始化numChecks,因此每次运行程序时操作中使用的值都是未知的,因为编译器会将变量设置为某个默认值。一些编译器将变量初始化为零;但是,其他编译器将其初始化为某些&#34; unknown &#34;值。

在编译器执行数学运算并在numChecks中存储不正确的值后初始化变量chargeXtotal,因此,为您提供了奇怪的值作为输出。 / p>

您应该更改代码,以便在完成numChecks的数学运算之前输入chargeXtotal

int numChecks;

cout << "Please enter how many checks you have used.\n";
cout << "and I will tell you how much your charge is. ";
cin >> numChecks;

double charge1Total = numChecks * CHARGE_1;
double charge2Total = numChecks * CHARGE_2;
double charge3Total = numChecks * CHARGE_3;
double charge4Total = numChecks * CHARGE_4;

希望这有帮助!