如何修复Total bill变量的显示?

时间:2016-02-24 03:20:19

标签: c++ project

以下是输出结果:

           Roller Coaster City Amusement Park

        Enter number of Children Tickets or -1 to stop...10
        Enter number of Adult Tickets......11


           Roller Coaster City Amusement Park
                 -------------------------

               Tickets      Price      Total
     Children    10         10.00     100.00
     Adults      11         25.00     275.00

                 21
   Security Fee                        15.00

     Total Bill                       410.00

          Cash Received........

总账单应该输出390.00。

这是我的C ++代码:

 // Author:         Bart Allen
// Source file:     amuseOne.cpp
/*
Description:     A program designed to output the number of tickets being purchased 
                for the entry into the amusement park
*/  
// IDE used:    Visual Studio 2015 

#include <iostream>
#include <iomanip>

using namespace std;

//the constants of the function 
const double CHILDPRICE = 12.00;
const double CHILDDISC = 10.00;
const double ADULTPRICE = 25.00;
const double Secfee = 15.00;


int main ()
{

    //appropiate variable declarations
    double childTotalDisc;
    int childTix;
    int adultTix;
    double childTotal;
    double adultTotal;
    double totalBill;
    double payment;
    double change;
    int totalTickets;
    int confirmNUM = 100;
    //Outputs the title of the amusement park
    cout << "\n           Roller Coaster City Amusement Park" << endl << endl;

    cout << "\tEnter number of Children Tickets or -1 to stop...";
    cin >> childTix;
    while (childTix != -1)
    {
        cout << "\tEnter number of Adult Tickets......";
        cin >> adultTix;



        //Calculations are placed here 
        //childTotalDisc = childTix * CHILDDISC;
        childTotal = childTix * CHILDPRICE;
        adultTotal = adultTix * ADULTPRICE;
        totalBill = childTotal + adultTotal;
        totalTickets = childTix + adultTix;


        //Sets the decimal place to only display two decimal places 
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);
        //Outputs the number of tickets being purchased, along with the calculated totals 
        cout << "\n\n           Roller Coaster City Amusement Park";
        cout << "\n                 -------------------------";
        cout << "\n\n               Tickets      Price      Total\n";
        if (childTix >= 8)
        {
            childTotal = CHILDDISC * childTix;
            cout << "     Children   " << setw(3) << childTix
                                       << setw(14) << CHILDDISC
                                       << setw(11) << childTotal;
        }
        else
        {
            cout << "     Children   " << setw(3) << childTix
                                       << setw(14) << CHILDPRICE
                                       << setw(11) << childTotal;
        }


        cout << "\n     Adults   " << setw(5) << adultTix
                                   << setw(14) << ADULTPRICE
                                   << setw(11) << adultTotal;
        cout << "\n\n            " << setw(7) << totalTickets;

        if ((totalTickets >= 20) || (childTix >= 14))
        {
            cout << "\n   Security Fee  " << setw(27) << Secfee;
            cout << "\n\n     Total Bill  " << setw(27) << totalBill + Secfee;
        }
        else
        {
            cout << "\n\n     Total Bill  " << setw(27) << totalBill;
        }

        //asks for the users input of amount of money being submitted to cover the final cost 
        cout << "\n\n\t  Cash Received........";
        cin >> payment;
        do
        {
            cout << "\tCash must be >= Total Bill ";

            cout << "\n\n\t  Cash Received........";
            cin >> payment;
        } while (payment < totalBill);
        change = payment - totalBill;
        cout << "\n     Change     " << setw(28.5) << change << endl;
        cout << "     Confirmation number =  " << confirmNUM++ << endl;

        cout << "\n    Enter number of Children Tickets or -1 to stop...";
        cin >> childTix;
    }
    system("pause");
    return 0;

}

字面上这是我对这个程序的唯一问题,幸运的是没有发生错误,这是我的项目!任何建议都会有帮助!

1 个答案:

答案 0 :(得分:2)

您已使用从totalBill计算的childTotal进行CHILDPRICE计算,之后您将childTotal更改为CHILDDISC,并忘记相应地更新totalBill

因此,我建议您进行childTix >=8检查,在那里计算其他所有内容,并将剩下的代码留给显示内容而不再进行任何计算。

替换:

childTotal = childTix * CHILDPRICE;
adultTotal = adultTix * ADULTPRICE;
totalBill = childTotal + adultTotal;
totalTickets = childTix + adultTix;

使用:

if (childTix >= 8)
{
 childTotal = childTix * CHILDDISC;
}
else
{
 childTotal = childTix * CHILDPRICE;
}
adultTotal = adultTix * ADULTPRICE;
totalBill = childTotal + adultTotal;
totalTickets = childTix + adultTix;

从显示逻辑的位置删除childTotal = CHILDDISC * childTix;

修改

您只是显示totalBill + Secfee而未将Secfee实际添加到totalBill

所以替换:

if ((totalTickets >= 20) || (childTix >= 14))
{
    cout << "\n   Security Fee  " << setw(27) << Secfee;
    cout << "\n\n     Total Bill  " << setw(27) << totalBill + Secfee;
}
else
{
    cout << "\n\n     Total Bill  " << setw(27) << totalBill;
}

使用:

if ((totalTickets >= 20) || (childTix >= 14))
{
    cout << "\n   Security Fee  " << setw(27) << Secfee;
    totalBill += Secfee;
    cout << "\n\n     Total Bill  " << setw(27) << totalBill;
}
else
{
    cout << "\n\n     Total Bill  " << setw(27) << totalBill;
}

此外,您应该在此处使用while而不是do while,否则即使输入了正确的输入也会重新输入,因为do while至少要运行一次。< / p>

所以替换:

cout << "\n\n\t  Cash Received........";
cin >> payment;
do
{
    cout << "\tCash must be >= Total Bill ";

    cout << "\n\n\t  Cash Received........";
    cin >> payment;
} while (payment < totalBill);

使用:

cout << "\n\n\t  Cash Received........";
cin >> payment;
while (payment < totalBill)
{
    cout << "\tCash must be >= Total Bill ";

    cout << "\n\n\t  Cash Received........";
    cin >> payment;
}

<强> EDIT2:

如果您仍希望使用do while,请执行以下操作:

//cout << "\n\n\t  Cash Received........"; remove this line
//cin >> payment; remove this line
do
{
    cout << "\n\n\t  Cash Received........";
    cin >> payment;
    if(payment < totalBill)
    {
     cout << "\tCash must be >= Total Bill ";
    }
}
while (payment < totalBill);