-1

我对 C++ 非常陌生,正在尝试完成一个程序,该程序将显示以下内容: 1. 所有客户账单的总数 2. 征收的税款总额 3. 客户数量 4. 平均客户账单。

平均账单、总税额和客户数量似乎都运行良好。我相信是 totalBill 变量把它扔掉了。下面附上代码,看不懂!

#include <iostream>
#include <iomanip>
using namespace std;

int main () {

double mealPrice;
double mealTotal;
double totalBills;
double totalTax;
double mealTax;
double averageBill;

int customerCount = 0;
mealTotal = 0.0;

bool anotherMeal = true;
char response;

while (anotherMeal == true) 
{
cout << "Enter price of your meal: ";
cin >> mealPrice;
cout << endl;
customerCount++;

cout << "Another cusotmer? y/n : ";
cin >> response;        
cout << endl << endl;
if (response == 'n') anotherMeal = false;
} //End While Loop

mealTax = (mealPrice * 0.0575);
mealTotal = (mealPrice + mealTax);
totalBills = (mealTotal += mealTotal);
totalTax = (mealTax + mealTax);
averageBill = (totalBills / customerCount);

cout << fixed << setprecision(2) << right;
cout << "Total Customer Bills :  $ " << setw(8) << right << totalBills << endl;
cout << "Total Tax Collected :   $ " << setw(8) << right << totalTax << endl;
cout << "Customer Count : " << setw(16) << right << customerCount << endl;
cout << "Average Customer Bill : $ " << setw(8) << right << averageBill << endl;
cout << endl;
cout << endl;

return 0;

} //End Main

当遵守时,它只给出正确的数字,只有一个客户,如果更多,总数将被抛出。提前致谢!

4

1 回答 1

2
totalBills = (mealTotal += mealTotal);

这条线没有任何意义。您正在将当前账单添加到当前账单;您想要的是将当前账单添加到运行总计中。

totalBills += mealTotal;

类似的改变也需要应用totalTax

此外,您的while循环结束得太早了。为了运行总计,计算需要在循环内进行。仅在循环中收集数据不会将账单添加到总数中。

于 2013-09-14T04:01:45.040 回答