我是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
如果符合规定,只有一个客户提供正确的数字,如果有更多的总数将被抛弃。提前谢谢!
答案 0 :(得分:2)
totalBills = (mealTotal += mealTotal);
这条线没有任何意义。您正在将当前账单添加到当前账单中;你想要的是将当前账单添加到运行总计中。
totalBills += mealTotal;
类似的更改也需要应用于totalTax
。
此外,您的while
循环过早结束。对于要运行的运行总计,计算需要在内部循环中进行。只是在循环中收集数据将不将账单添加到总数中。