我不确定我的一个公式是否错误,我是否在错误的区域放置了一些东西,或者我完全错过了一些东西。但是当我运行编。它会将前一张顾客餐桌的餐费加起来,而不是为每张新餐桌重新设置。有什么帮助吗?
int main ()
{
//These are the variables used for the formulas and inputs.
int people, counter;
float price, subtotal, tip, tax, total;
cout<<"How many people are at the table?" <<endl;
cin>>people;
//Use a while statement to start a loop
while (people!=0)
{
//Use a for statement inside the while to make a nested loop. It will ask the price of each meal.
for(counter=1; counter<=people; counter++)
{
cout<<"How much is the meal?: " <<endl;
cin>>price;
subtotal+=price;
tax=subtotal*.06;
if (people<5)
{
tip=subtotal*.18;
}
else
tip=subtotal*.20;
total=tax+subtotal+tip;
}
//This is the final output for the program. Which will be the bill.
cout<<setprecision(2) <<fixed;
cout<<left;
cout<<setw(20)<<"Subtotal: " <<"$" <<subtotal <<endl;
cout<<setw(20)<<"Sales Tax: " <<"$" <<tax <<endl;
cout<<setw(20)<<"Tip: " <<"$" <<tip <<endl;
cout<<setw(20)<<"Total: " <<"$" <<total <<endl;
cout<<" " <<endl;
cout<<setw(20)<<"How many people are at the table?" <<endl;
cin>>people;
}