所以这是输出:

           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........

The total bill is supposed to output 390.00.

Here's my C++ code:

 // 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;

}


从字面上看,这是该程序的唯一问题,幸运的是没有发生任何错误,这是我的项目!任何建议将有所帮助!

最佳答案

您已经使用从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至少要运行一次。

因此,请替换:

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


编辑2:

如果您仍然希望使用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);

10-08 13:56