私は C++ に非常に慣れていないので、以下を表示するプログラムを作成しようとしています。
平均請求額、総税額、および顧客数はすべて問題なく機能しているようです。私が信じているのは、それを捨てているのは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
従うと、顧客が 1 人だけの場合にのみ正しい数値が得られます。それ以上の場合は、合計が破棄されます。前もって感謝します!