如何打印执行while循环的次数?

时间:2018-11-07 16:27:34

标签: c

我正在为C中的一家餐馆开发程序:

#include <stdlib.h>
#include <stdio.h>
#define CAPPUCCINO_PRICE 129.00
#define FILTERCOFFEE_PRICE 110.00
#define CAFELATTE_PRICE 128.00
#define ESPRESSO_PRICE 135.00
#define MACCHIATO_PRICE 137.00
#define CAFEMOCHA_PRICE 130.00
#define VANILLALATTE_PRICE 140.00
#define ASSAMTEA_PRICE 120.00
#define MASALACHAI_PRICE 120.00
#define GREENTEA_PRICE 126.00
#define LEMONADE_PRICE 147.00
#define COOLBLUE_PRICE 154.00
#define CAFEFRAPPE_PRICE 161.00
#define VEGANSHAKE_PRICE 198.00
#define COLDCOFFEE_PRICE 161.00
#define CRUNCHYFRAPPE_PRICE 155.00
#define TOFFEESMOOTHIE_PRICE 182.00
#define DETOXWATER_PRICE 127.00
#define BLUESPARKLER_PRICE 161.00
#define VIRGINMOJITO_PRICE 198.00
int main()
{

    int choice = 0;
    double totalPrice = 0;

    printf("Welcome to delTaco restaurant!\n");
    printf("May I take your order? \n\n");

    do{
        printf("Menu: \n\n");
        printf("1.  Cappuccino       Rs %.2lf \n", CAPPUCCINO_PRICE);
        printf("2.  Filter Cofee     Rs %.2lf \n", FILTERCOFFEE_PRICE);
        printf("3.  Cafe Latte       Rs %.2lf \n", CAFELATTE_PRICE);
        printf("4.  Espresso         Rs %.2lf \n", ESPRESSO_PRICE);
        printf("5.  Macchiato        Rs %.2lf \n", MACCHIATO_PRICE);
        printf("6.  Cafe Mocha       Rs %.2lf \n", CAFEMOCHA_PRICE);
        printf("7.  Vanilla Latte    Rs %.2lf \n", VANILLALATTE_PRICE);
        printf("8.  Assam Tea        Rs %.2lf \n", ASSAMTEA_PRICE);
        printf("9.  Masala Chai      Rs %.2lf \n", MASALACHAI_PRICE);
        printf("10. Green Tea        Rs %.2lf \n", GREENTEA_PRICE);
        printf("11. Lemonade         Rs %.2lf \n", LEMONADE_PRICE);
        printf("12. Cool Blue        Rs %.2lf \n", COOLBLUE_PRICE);
        printf("13. Cafe Frappe      Rs %.2lf \n", CAFEFRAPPE_PRICE);
        printf("14. Vegan Shake      Rs %.2lf \n", VEGANSHAKE_PRICE);
        printf("15. Cold Coffee      Rs %.2lf \n", COLDCOFFEE_PRICE);
        printf("16. Crunchy Frappe   Rs %.2lf \n", CRUNCHYFRAPPE_PRICE);
        printf("17. Toffee Smoothie  Rs %.2lf \n", TOFFEESMOOTHIE_PRICE);
        printf("18. Detox Water      Rs %.2lf \n", DETOXWATER_PRICE);
        printf("19. Blue Sparkler    Rs %.2lf \n", BLUESPARKLER_PRICE);
        printf("20. Virgin Mojito    Rs %.2lf \n", VIRGINMOJITO_PRICE);
        printf("21. Exit\n\n");
        printf("Enter an item: ");
        scanf("%i", &choice);

        switch(choice){
        case 1:
            totalPrice += CAPPUCCINO_PRICE;
            break;
        case 2:
            totalPrice += FILTERCOFFEE_PRICE;
            break;
        case 3:
            totalPrice += CAFELATTE_PRICE;
            break;
        case 4:
            totalPrice += ESPRESSO_PRICE;
            break;
        case 5:
            totalPrice += MACCHIATO_PRICE;
            break;
        case 6:
            totalPrice += CAFEMOCHA_PRICE;
            break;
        case 7:
            totalPrice += VANILLALATTE_PRICE;
            break;
        case 8:
            totalPrice += ASSAMTEA_PRICE;
            break;
        case 9:
            totalPrice += MASALACHAI_PRICE;
            break;
        case 10:
            totalPrice += GREENTEA_PRICE;
            break;
        case 11:
            totalPrice += LEMONADE_PRICE;
            break;
        case 12:
            totalPrice += COOLBLUE_PRICE;
            break;
        case 13:
            totalPrice += CAFEFRAPPE_PRICE;
            break;
        case 14:
            totalPrice += VEGANSHAKE_PRICE;
            break;
        case 15:
            totalPrice += COLDCOFFEE_PRICE;
            break;
        case 16:
            totalPrice += CRUNCHYFRAPPE_PRICE;
            break;
        case 17:
            totalPrice += TOFFEESMOOTHIE_PRICE;
            break;
        case 18:
            totalPrice += DETOXWATER_PRICE;
            break;
        case 19:
            totalPrice += BLUESPARKLER_PRICE;
            break;
        case 20:
            totalPrice += VIRGINMOJITO_PRICE;
            break;
        case 21:
            printf("Than you for ordering at delTaco Restaurant! \n");
            break;
        default:
            printf("*** Error:  This is not on the menu. \n");
            break;
        }

        printf("Total so far: Rs %.2lf\n\n", totalPrice);

    } while (choice != 21);

    printf("Your order is Rs %.2lf \n\n", totalPrice);
    printf("Thank you for coming to delTaco Restaurant.\n");
    printf("Have a nice day!.\n\n");

    system("pause");
    return 0;
}

用户必须输入为食品项目定义的数字,一旦按回车键,就会显示该食品项目的总金额,但不会显示所选项目的数量,

Image 1 shows the interface

Image 2 shows the total amount calculated

In image 3, i tried calculating the number of items selected but failed.

我该怎么做?

1 个答案:

答案 0 :(得分:0)

类似的事情应该做。

更改

do{
    printf("Menu: \n\n");

int items_in_order = 0;
do{
    ++items_in_order;
    printf("Menu: \n\n");

和  改变这个

    case 21:
        printf("Than you for ordering at delTaco Restaurant! \n");
        break;
    default:
        printf("*** Error:  This is not on the menu. \n");
        break;
    }

    printf("Total so far: Rs %.2lf\n\n", totalPrice);

    case 21:
        printf("Than you for ordering at delTaco Restaurant! \n");
        --items_in_order;
        break;
    default:
        printf("*** Error:  This is not on the menu. \n");
        --items_in_order;
        break;
    }

    printf("Total so far: Rs %.2lf\n\n", totalPrice);
    printf("No. of items far: %d\n\n", items_in_order);