if语句处理不正确,计算输出错误

时间:2012-02-09 18:30:45

标签: c++ if-statement

我无法让它继续发布,所以我想我会重新发布。 我按照建议改变了switch语句(我以为你可以沿着那条线编码)。在需要时,程序不会脱离if语句。它通过所有三个并给出不正确的结果。 (我的数学可能编码错误)。这是修改后的程序。

程序在执行时询问包裹和使用的时间。然后它为所有包裹提供不正确费率的费率。 IE - >套餐A应为9.95。程序输出为“-.95”,“B& C为9.95”

程序应该只转到所选包的正确if语句,并给出基本费率(packageA)和使用率(overPackageA)的费率基础

// Purpose: It should ask which package the customer has purchased and how many
//           hours were used. It should then display the total amount due. 
//           Input Validation: Be sure the user only selects package A, B, or C. 
//           Also, the number of hours used in a month cannot exceed 744.

# include <iostream>
# include <cstring>
# include <iomanip>

using namespace std;

main()
{

    const int maximumHours = 774;
    char customerPackage;
    double hoursUsed = 0,
           packageA = 9.95,
           packageB = 14.95,
           packageC = 19.95,
           overPackageA = 2.00,
           overPackageB = 1.00,
           overTime = 0,
           amountDue = 0,
           excessCharged = 0;

    cout << "Please enter the customer's package: ";
    cin >> customerPackage;

    switch (customerPackage)
     {
            case 'a':
            case 'A':
            case 'b':
            case 'B':
            case 'c':
            case 'C':
                cout << "Please enter the number of hours used: ";
                cin >> hoursUsed;
                break;
                default:
                    cout << "Error "
                         << " Please enter the customer's purchased package: ";
                      cin >> customerPackage;
     }    

    if ( (customerPackage == 'a' || customerPackage == 'A') && hoursUsed <= 10)           

        amountDue = packageA;
        else 

        {
            overTime = packageA - hoursUsed;
            excessCharged = overTime * overPackageA;
            amountDue = packageA + excessCharged;


        cout << fixed << showpoint << setprecision(2);
        cout << "Customer is charged for package A and has the amount of: " 
         << amountDue << endl; 

        }

    if ( (customerPackage == 'b' || customerPackage == 'B') && hoursUsed <= 20)           

        amountDue = packageB;

        else
       {
            overTime = packageA - hoursUsed;
            excessCharged = overTime * overPackageA;
            amountDue = packageA + excessCharged;


        cout << fixed << showpoint << setprecision(2);
        cout << "Customer is charged for package B and has the amount of: " 
             << amountDue << endl; 
    }    
    if ( (customerPackage == 'c' || customerPackage == 'C') && hoursUsed >= 0)

        amountDue = packageC;

        {
         cout << fixed << showpoint << setprecision(2);
         cout << "Customer is charged for package C and has the amount of: " 
            << amountDue << endl; 
        }

    system("pause");
    return 0;
}

0 个答案:

没有答案