gpt4 book ai didi

c++ - while true 循环函数

转载 作者:行者123 更新时间:2023-11-30 19:55:59 26 4
gpt4 key购买 nike

据我了解,while 循环可以这样完成:

while (true) {
// run this code
if (condition satisfies)
break; // return;
}

所以我接受了它,并在下面放入了以下主要函数,我的问题是,当我在满足条件(5或更多)后放入 printf 语句时,它会停止并且不显示任何内容,如果我除了break语句之外什么都不放,它可以工作并且循环5个不同的客户!我是否遗漏了有关 while true 循环的任何内容,或者这就是它的工作方式?

int main ()
{
displayMenu();
while (true)
{
int gasType,gallonsAmount,carWashOption,customerAmount ;
float gasPrices[SIZE]={2.99,3.099,3.199,3.299}, washPrices[SIZE]=
{3.50,3.00,3.00,2.50}, perGallonRate,perWashRate,total;
float totalSum,totalGallonSum;
gasType = getGasType();
gallonsAmount = getGallons();
carWashOption = getCarWash();
perGallonRate = getGallonRate(gasPrices, gasType);
if (carWashOption == 'Y')
{
perWashRate = getWashRate( washPrices, gasType );
total = calcAmount(gallonsAmount, perGallonRate, perWashRate,&totalSum, &totalGallonSum);
}
else
{
perWashRate = 0;
total = calcAmount(gallonsAmount, perGallonRate, perWashRate,&totalSum, &totalGallonSum);
}
customerAmount++;// once we reach a total of 5 customers(Assume its the end of the day), the loop will break and give me a summary.
if (customerAmount >= 5)
printf("totalSum: %.2f ", totalSum ,totalGallonSum);
break;
}

return 0;
}

最佳答案

 if (customerAmount >= 5)
printf("totalSum: %.2f ", totalSum , totalGallonSum );
break;

break 不是 if 的一部分。因此循环第一次结束。使用大括号。

 if (customerAmount >= 5)
{
printf("totalSum: %.2f ", totalSum , totalGallonSum );
break;
}

关于c++ - while true 循环函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44347370/

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