gpt4 book ai didi

C:似乎找不到错误[Do-While 循环]

转载 作者:行者123 更新时间:2023-11-30 19:58:45 24 4
gpt4 key购买 nike

程序很简单:

1) 它接受用户名2)询问他购买了多少件元素以及花费了多少3)如果输入任何> 10个项目,它会说“请输入一个大于0(零)且小于10(十)的数字。您想继续生成另一个账单吗?(是/否)”4) 用户输入“Y”,应该将他带到 do 循环的开头并重新开始。

两者之间的一切都工作正常。这是代码:

int main()
{
char item[10][10], answer = 'null', name[10];
int price[10], total_item, total_price = 0, i, j = 0, a;
float service_tax = 0, vat = 0,total_bill = 0, bill = 0;

printf ("Please enter your name: ");
scanf ("%s", name);

do
{
printf ("\n Enter the total items purchased (must be more than 0 (zero) and less than 10 (ten): ");
scanf (" %d", &total_item);

if(total_item > 0 && total_item <= 10)
{
for(i = 0; i < total_item; i++)
{
printf ("\nEnter the item name: ");
scanf ("%s", item[i]);

printf ("\nEnter the item price: ");
scanf ("%d", &price[i]);

bill = bill + price[i];
}

printf("You bill WITHOUT the tax is: %f \n", bill);

service_tax = ((bill * 10)/100);
vat = ((bill * 12)/100);

total_bill = service_tax + vat + bill;

printf("The items you've purchased are: \n");
for(i = 0; i < total_item; i++)
{
printf("\n%s - %d\n", item[i], price[i]);
}

printf("Your total bill is: %3f", total_bill);

printf ("Your bill# is %s-%d-%d", &name, total_item, rand()%1000);
}

else
{
printf("\n %s, please enter a number greater than 0 (zero) and less than 10 (ten)", &name);

printf("\nDo you want to continue generating another bill? (Y/N)\n");
scanf (" %c", &answer);
}

}while ((answer == 'y') || (answer = 'Y'));

return 0;}

请帮帮我!对于我的一生,我似乎无法弄清楚为什么 do-while 循环不起作用。

非常感谢您的帮助!

最佳答案

这个:

while ((answer == 'y') || (answer = 'Y'));

使用 = 表示 ==。上述条件始终为真,因为 'Y' 不为零。

有些人通过始终将变量写在右侧来防止自己遇到此问题,即上面的内容应该(正确)写为

while (('y' == answer) || ('Y' == answer));

当这样写时,如果你错过了一个=,你会得到一个错误,因为左边的文字是不可分配的。

我不喜欢这个,我觉得很难读。当比较两个变量时它也会崩溃,这并不罕见。

关于C:似乎找不到错误[Do-While 循环],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21880408/

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