程序很簡單:C:似乎無法找到的bug [do-while循環]
1)它接受用戶的姓名 2)問有多少項目,他已經購買了多少它的成本(Y/N)「 3)如果輸入任何東西> 10項,它會顯示」請輸入一個大於0(零)且小於10(十)的數字。是否要繼續生成另一個帳單?用戶輸入'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循環無法正常工作。
非常感謝您的幫助!
answer ='Y'更改爲回答=='Y' – sashkello
'answer ='Y''在'while'子句中可疑,您的意思是'answer =='Y' '? – Teetoo