我如何从循环中清除第一个输入以正确计算第二个输入?

时间:2018-07-20 13:37:05

标签: c

显然,我正在运行一个程序,它应该创建一个餐厅,包括菜单,客户,报告等。

因此,我首先执行了customer函数,然后将其循环(customer 1,customer2,customer3),但是在完成对customer 1的计算(计算cus1的总数)之后,我继续循环并继续处理customer 2.但是这个问题出现了,我的客户2的总数与客户1的混合在一起(为了更好地说明,客户2的总数是cus1 + cus2,但我只希望客户2的总数)

如果有人知道,请给我一个指南,将不胜感激,谢谢!

#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#define COMBOA 8.50
#define COMBOB 10.50
#define COMBOC 18.00
#define COMBOD 32.50

void main()
{
    void logo();
    void menu();
    char cus();

    logo();
    menu();
    cus();

    system("pause");
}

void logo() //Restaurant's logo
void menu() //Restaurant's menu

char cus()
{
    char combo;
    double total, combo1, total2 = 0;
    int quan, cus = 1;

    do
    {
        printf("Customer No: %d\n\n", cus);
        ++cus;

        do {
            printf("Please select Combo A/B/C/D (Enter 'X' to exit) : ");
            scanf(" %c", &combo);
            fflush(stdin);

            if (combo == 'X' || combo == 'x')
                break;
            printf("Quantity : ");
            scanf(" %d", &quan);

            switch (combo) //about the combo prices

            total = (double)quan * combo1;
            total2 += total;

            if (combo != 'A' && combo != 'a' && combo != 'B' && combo != 'b' && combo != 'C' && combo != 'c' && combo != 'D' && combo != 'd')
                total2 == total;

            printf("\t\tCOMBO %c : %d @ RM%.2f = RM %.2f\n", combo, quan, combo1, total);
     } while (combo != 'X' || combo != 'x');

        printf("\t\tTOTAL AMOUNT PURCHASED = RM %.2f\n", total2);

 return 0;

}

2 个答案:

答案 0 :(得分:1)

您有一些变量将在循环的每次迭代中重置,然后仅在同一迭代中使用,然后再次重置它们。在循环中,或者更好的是,在使用它们的块中声明它们。

在这种情况下,那将是包含设置它们的scanf()语句的块。

(顺便说一下,处理美元和美分的代码始终使用定点而不是32位浮点,以避免舍入错误。)

答案 1 :(得分:0)

 if (combo != 'A' && combo != 'a' ...)
           total2 == total;

total2无影响。替换为“ total2 = total;”