该计划很简单:
1)它接受用户的名字2)询问他购买了多少物品以及花费多少3)如果输入任何> 10项,则表示"请输入大于0(零)且小于10(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));
如果这样写,如果你错过了一个,=你会得到一个错误,因为左边的文字是不可分配的.
我不喜欢这个,我觉得很难读.在比较两个变量时它也会崩溃,这并不罕见.