gpt4 book ai didi

c++ - 我的值不断增加,而不是在我的循环语句中重置

转载 作者:行者123 更新时间:2023-11-28 07:52:02 25 4
gpt4 key购买 nike

我不确定我的某个公式是否有误,是我把某些东西放在了错误的区域,还是我完全遗漏了某些东西。但是当我运行编。它会把前一张顾客餐 table 的餐费加起来,而不是为每张新餐 table 重新设置。有帮助吗?

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;

}

最佳答案

您想在 while 循环内和 for 之前将所有变量重置为 0,尤其是 subtotal

关于c++ - 我的值不断增加,而不是在我的循环语句中重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13576216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com