开关案例菜单未执行,只读取默认案例

时间:2017-05-14 08:35:12

标签: c function switch-statement

我对C语言还不熟悉。我的问题是函数switch中的getDiscount(int ch, float total)只读取默认情况并执行代码。我无法识别任何错误,因为它正确运行,但却产生了错误的答案。这是我的完整代码。我的scanf语句是否采用其ASCII值并执行?我不知道。

#include<stdio.h>

float getTotalPrice(char type, int qty);

float getDiscount(char mode, float total);

void getGift(float total);

int main(void)
{
    int quantity;

    float total;

    char garment, method;

    printf("\nAvailable Garment Types:\n");
    printf("Garment Type\tPrice\\u\n");
    printf("T-Shirt - 't'\tRs. 1500.00\n");
    printf("Frock - 'f'\tRs.3500.00\n");
    printf("Skirts - 's'\tRs.2000.00\n");

        printf("\nEnter Your Choice: ");
        scanf("%c", &garment);

        printf("Enter the quantity: ");
        scanf("%d", &quantity);

        printf("\nAvailable payment methods are:\n");
        printf("Cash - 'c'\nCredit Card - 'd'\nOther - 'o'\n"); 

        printf("\nEnter Your Payment Method: ");
        scanf("%c", &method);

        total = getTotalPrice(garment,quantity);

        total = getDiscount(method,total);

        getGift(total);

    return 0;
}

float getTotalPrice(char type, int qty)
{
    float total=0;

        switch(type)
        {
                case 't':
                case 'T':
                                total = 1500*qty;
                                break;

                case 'f':
                case 'F':
                                total = 3500*qty;
                                break;

                case 's':
                case 'S':
                                total = 2000*qty;
                                break;
                default:
                                printf("\nError: Enter a valid choice\n");
                                break;
        }

        return total;

}


float getDiscount(char mode, float total)
{
        switch(mode)
        {
                case 'c':
                case 'C':
                                total = (total - (total*15/100));
                                break;

                case 'd':
                case 'D':
                                total = (total - (total*10/100));
                                break;

                case 'o':
                case 'O':
                                total = (total - (total*5/100));
                                break;

                default:
                                printf("\nError: Enter a valid payment method\n");
                                break;

        }

                return total;
}

void getGift(float total)
{
        float gift;

        if(total >= 10000)
        {
                gift = 3000;
        }

        else if((total >= 5000)&&(total <= 9999))
        {
                gift = 2000;
        }

        else if((total < 4999)&&(total > 1))
        {
                gift = 1000;
        }

        else
        {
                printf("\nError: Invalid inputs\n");
        }

        printf("\nTotal Price = Rs. %.f\n", total);

        printf("\nYour gift voucher value = Rs. %.f\n", gift);
}

1 个答案:

答案 0 :(得分:0)

在您的scanf中添加空格scanf("%c", &method);,这样就会变成scanf(" %c", &method);

为什么会发生这种情况?您始终使用上一个getdiscount的输入输入scanf()函数,这会破坏您的输入,即{{1}之前的空格将忽略该输入,从而让您选择正确的字符 - %c

相关问题