如何修复 Total bill 变量的显示?
How do I fix the display of the Total bill variable?
输出结果如下:
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;
}
从字面上看,这是我对该程序的唯一问题,幸运的是没有发生任何错误,这是我的项目!任何建议都会有所帮助!
您已经使用 childTotal
完成了 totalBill
计算,从 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);
输出结果如下:
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;
}
从字面上看,这是我对该程序的唯一问题,幸运的是没有发生任何错误,这是我的项目!任何建议都会有所帮助!
您已经使用 childTotal
完成了 totalBill
计算,从 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);