case 命令后无法计算折扣值

discount value cannot calculate after case commad

我的作业有问题。这就是它的样子

#include<stdio.h>
int main()
{
    char code;
    int price,discount;
    float total;
    printf("Please input price: ");
    scanf("%d",&price);
    printf("Please input discount code: ");
    scanf(" %c",&code);
    switch(code)
    {
        case 'a':   printf("Your discount code is 25 percent\n");
                    discount = 25;
                    break;
        case 'b':   printf("Your discount code is 15 percent\n");
                    discount = 15;
                    break;
        case 'c':   printf("Your discount code is 5 percent\n");
                    discount = 5;
                    break;
        default:    printf("Wrong code,Your discount is 0 percent\n");
                    discount = 0;
                    break;
    }
    total = (price*((100-discount)/100));
    printf("Your price is = %.2f\n",total);
}

我有2个问题要问

  1. 我的任务是我必须输入大写和小写字母的折扣代码(只有三个代码:a,b,c)所以我怎么把它们都放在 case 命令中? (这里我只做小写字母)

  2. 我有运行这个。但是当我最后尝试使用它进行计算时,折扣值似乎为 0。当我只打印折扣时,它工作正常。我该如何解决?

抱歉我的英语不好,谢谢你的帮助!

会有不同的可能性。

scanf(" %c",&code);
switch(code)
{
    case 'a':
    case 'A':
      printf("Your discount code is 25 percent\n");
      discount = 25;
      break;

或者您在切换前或切换中将输入更改为小写!

switch( code | 0x60 ) // make it to lower case

有了这个,您不必更改以下代码。