计算时,值始终返回0

时间:2014-05-13 14:02:09

标签: c++ variable-assignment

我正在为我的主题做一个作业,在作业中,每个学生都需要创建一个简单的C ++程序。这里的问题是,当我没有为变量赋值时,会发生编译错误。但是,当我指定默认值(在这种情况下,值= 0)时,该值将始终为0.

我的问题是,有没有办法解决这个问题而没有遇到编译错误?

如果有类似的问题,请问您也可以包含解决问题的链接吗?非常感谢你!

[编辑#1 :对于那些想知道为什么代码“混乱”的人,我只使用简单的命令而没有使用面向对象的组件,if / else语句等。项目创建的是意思是由“简单代码”组成。]

[编辑#2 :这是我拍摄的输出显示: http://i.imgur.com/4csk1Rz.png

总折扣费用部分应显示数字而非默认值0]

(Microsoft Visual Studio Pro 2013; C ++)

预期输出显示: http://i.imgur.com/jOJvymV.png

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    /* Declare variables */
    int qtyVehicleCar, qtyVehicleTaxi, qtyVehicleTruck; // User input

    double feeOriginalCar, feeTotalOriginalCar, rateDiscountCar, feeTotalDiscountedCar, feeTotalAfterDiscountedCar; // variables for vehicle Car
    double feeOriginalTaxi, feeTotalOriginalTaxi, rateDiscountTaxi, feeTotalDiscountedTaxi, feeTotalAfterDiscountedTaxi; // variables for vehicle Taxi
    double feeOriginalTruck, feeTotalOriginalTruck, rateDiscountTruck, feeTotalDiscountedTruck, feeTotalAfterDiscountedTruck; // variables for vehicle Truck
    double feeRepTotalOriginalFee, feeRepTotalDiscountedFee, feeRepTotalAfterDiscounted; // Generate final output

    cout << "=====================================\n";
    cout << "=====================================\n";
    cout << "====== Malaysia Highway Company =====\n";
    cout << "=====================================\n";
    cout << "=====================================\n";
    cout << endl;

    cout << "Enter quantity of the vehicles on 1st January 2014: " << endl;
    cout << "Car\t\t:\t";
    cin >> qtyVehicleCar;

    cout << "Taxi\t\t:\t";
    cin >> qtyVehicleTaxi;

    cout << "Truck\t\t:\t";
    cin >> qtyVehicleTruck;

    /* Lay out the Report table */
    cout << endl << endl << endl;
    cout << "Report\n";
    cout << "--------\n";
    cout << endl;

    /* ============================== */
    /* Declaration prior to process */
    /* ============================== */

    /* The problematic part
    feeTotalDiscountedCar = 0;
    feeTotalDiscountedTaxi = 0;
    feeTotalDiscountedTaxi = 0;
    */

    // Prices
    feeOriginalCar = 2.00;
    feeOriginalTaxi = 1.00;
    feeOriginalTruck = 3.50;

    feeTotalOriginalCar = feeOriginalCar * qtyVehicleCar;
    feeTotalOriginalTaxi = feeOriginalTaxi * qtyVehicleTaxi;
    feeTotalOriginalTruck = feeOriginalTruck * qtyVehicleTruck;

    // In percentage
    rateDiscountCar = 2;
    rateDiscountTaxi = 10;
    rateDiscountTruck = 15;

    feeTotalAfterDiscountedCar = (rateDiscountCar/100) * feeTotalOriginalCar;
    feeTotalAfterDiscountedTaxi = (rateDiscountTaxi/100) * feeTotalOriginalTaxi;
    feeTotalAfterDiscountedTruck = (rateDiscountTruck/100) * feeTotalOriginalTruck;

    // Final total - to be displayed
    feeTotalAfterDiscountedCar = feeTotalOriginalCar - feeTotalAfterDiscountedCar;
    feeTotalAfterDiscountedTaxi = feeTotalOriginalTaxi - feeTotalAfterDiscountedTaxi;
    feeTotalOriginalTruck = feeTotalOriginalTruck - feeTotalAfterDiscountedTruck;

    feeRepTotalOriginalFee = feeTotalOriginalCar + feeTotalOriginalTaxi + feeTotalOriginalTruck;
    feeRepTotalDiscountedFee = feeTotalDiscountedCar + feeTotalAfterDiscountedTaxi + feeTotalAfterDiscountedTruck;
    feeRepTotalAfterDiscounted = feeRepTotalOriginalFee - feeRepTotalDiscountedFee;

    /* ================= */
    /* Declaration END */
    /* ================= */

    /* Gives the following variables a default value, they will be modified later by the program */
    feeTotalAfterDiscountedTruck = 0;
    feeTotalDiscountedTruck = 0;

    /* Processes */
    cout << "Item\t\t\t\t" << "Car\t" << "Taxi\t" << "Truck\t" << endl;
    cout << "------------------------------------------------------\n";
    cout << "Original fee\t\t\t" << "RM " << feeOriginalCar << "\t" << "RM " << feeOriginalTaxi << "\t" << "RM " << feeOriginalTruck << endl;
    cout << "Quantity\t\t\t" << qtyVehicleCar << "\t" << qtyVehicleTaxi << "\t" << qtyVehicleTruck << endl;
    cout << "Total original fee\t\t" << "RM " << feeOriginalCar << "\t" << "RM " << feeOriginalTaxi << "\t" << "RM " << feeOriginalTruck << endl;
    cout << "Discount rate\t\t\t" << rateDiscountCar << "%\t" << rateDiscountTaxi << "%\t" << rateDiscountTruck << "%\t" << endl;
    cout << "Total discounted fee\t\t" << "RM " << feeTotalDiscountedCar << "\t" << "RM " << feeTotalDiscountedTaxi << "\t" << "RM " << feeTotalDiscountedTruck << endl;
    cout << "Total after discounted fee\t" << "RM " << feeTotalAfterDiscountedCar << "\t" << "RM " << feeTotalAfterDiscountedTaxi << "\t" << "RM " << feeTotalAfterDiscountedTruck << endl;
    cout << endl;

    /* Displays output */
    cout << "Total original fee\t\t:\t" << "RM " << feeRepTotalOriginalFee << endl;
    cout << "Total discounted fee\t\t:\t" << "RM " << feeRepTotalDiscountedFee << endl;
    cout << "Total after discounted fee\t:\t" << "RM " << feeRepTotalAfterDiscounted << endl;
    cout << endl << endl;

    /* Displays end line */
    cout << "------------------- End of Program -------------------" << endl;
    system("PAUSE");
    return 0;
}

3 个答案:

答案 0 :(得分:2)

未初始化的变量警告可能意味着(1)您忘记初始化某些内容或(2)您认为您已将其初始化但未进行初始化。在我看来,这是第二个问题。您的变量名称已混淆。

例如,

feeTotalAfterDiscountedCar = (rateDiscountCar/100) * feeTotalOriginalCar;
feeTotalAfterDiscountedTaxi = (rateDiscountTaxi/100) * feeTotalOriginalTaxi;
feeTotalAfterDiscountedTruck = (rateDiscountTruck/100) * feeTotalOriginalTruck;

这些应该是折扣金额,不是吗?那么为什么要将它们存储到feeTotalAfterDiscountedSomething而不是feeTotalDiscountedSomething

类似地,

feeTotalAfterDiscountedCar = feeTotalOriginalCar - feeTotalAfterDiscountedCar;
feeTotalAfterDiscountedTaxi = feeTotalOriginalTaxi - feeTotalAfterDiscountedTaxi;
feeTotalOriginalTruck = feeTotalOriginalTruck - feeTotalAfterDiscountedTruck;

为什么第三个将结果存储到feeTotalOriginalTruck而不是feeTotalAfterDiscountedTruck? (另请注意,如果你解决了上面的第一个问题,减法的rhs也需要改变)。

此外,

feeRepTotalDiscountedFee = feeTotalDiscountedCar + feeTotalAfterDiscountedTaxi + feeTotalAfterDiscountedTruck;

类似的问题。我相信,这些应该是feeTotalDiscountedSomething而不是feeTotalAfterDiscountedSomething

答案 1 :(得分:0)

编译器正确地抱怨feeTotalDiscountedCarfeeTotalDiscountedTaxi未初始化。你需要在某处初始化它们。也许你希望用户在这样的地方输入它们:

  cout << "Discount Fee Total Taxi:";
  cin >> feeTotalDiscountedTaxi;

  cout << "Discount Fee Total Car:";
  cin >> feeTotalDiscountedCar;

答案 2 :(得分:0)

为什么要在打印之前将它们设置为零?

feeTotalAfterDiscountedTruck = 0; feeTotalDiscountedTruck = 0;

它将显示错误,因为您尝试使用该变量而不进行初始化。