【问题标题】:Switch case menu isn't being executed and reads only the default case切换案例菜单未执行,仅读取默认案例
【发布时间】:2017-05-14 08:35:12
【问题描述】:

我对 C 语言还很陌生。这里我的问题是getDiscount(int ch, float total)函数中的switch case只读取默认case并执行代码。我无法识别任何错误,因为它运行正确,但产生了错误的答案。这是我的完整代码。我的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);
}

【问题讨论】:

  • 你试过调试吗?进入函数的时候检查mode的值了吗?
  • 这很简单。你的函数有一个参数“模式”。在你的 switch case 之前,添加一个 "printf("%c", mode);" (并且可能添加 while(1); 来停止执行),这将向您显示值......或者只是使用 GDB 或其他调试工具进行调试

标签: c function switch-statement


【解决方案1】:

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

为什么会发生这种情况?您总是在输入 getdiscount 函数的同时输入前一个 scanf(),这会破坏您的输入,%c 之前的空格将忽略该输入并从而让您选择正确的字符 - c, d,d or o

【讨论】:

    猜你喜欢
    • 2015-05-12
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    相关资源
    最近更新 更多