if-else结构怎么了?

时间:2019-10-02 20:10:21

标签: c

所以,我是一名初学者程序员,我正在尝试做一个练习,我必须创建一个程序来捕获价格和产品的源代码,并通过源代码显示该程序从何处显示。产品来自。因此,这是源代码列表:

  • 1 =南
  • 2 =北
  • 3 =东
  • 4 =西
  • 5〜6 =东北
  • 7〜9 =东南
  • 10〜20 =中西部
  • 25〜30 =东北

如果用户插入的源代码与此列表中的任何一个都不匹配,则会出现一条消息,指出插入的代码在列表中没有对应的内容。因此,我进行了练习,并提供了最终代码:

int main() {
    float price;
    int code;
    char check1, check2;

    printf("Hello there! Insert the price of your product: ");
    scanf("%f", &price);
    printf("\nAlright, now enter the source code of the product: ");
    scanf("%d", &code);

    switch(code) {
    case 1:
        printf("Your product came from the South");
        break;

    case 2:
        printf("Your product came from the North");
        break;

    case 3:
        printf("Your product came from the East");
        break;

    case 4:
        printf("Your product came from the West");
        break;

    default:
        if(code == 5 || code == 6 || (code >= 25 && code <= 30)) {
            printf("Your product came from the Northeast");
        } else {
            if(code >= 7 && code <= 9) {
                printf("Your product came from the Southeast");
            } else {
                if(code >= 10 && code <= 20) {
                    printf("Your product came from the Midwest");
                } else {
                    printf("Sorry, this code doesn't match with any correspondence");
                    return 0;
                }
            }
        }
        break;
    }

    printf(" and it costs %0.2f\n", price);
}

但是我还想在程序检查是否存在任何对应关系之前做一个if-else结构,以使用户有机会纠正他插入错误的内容或类似内容。所以我这样做了:

#include <stdio.h>
#include <stdlib.h>

int main() {
    float price;
    int code;
    char check1, check2;

    printf("Hello there! Insert the price of your product: ");
    scanf("%f", &price);
    printf("\nAlright, now enter the source code of the product: ");
    scanf("%d", &code);

    printf(
        "So, the price is %0.2f and the code is %d, right? Press c to continue, r "
        "to reset and any other key to quit: \n",
        price, code);
    scanf("%s", &check1);

    if(check1 == 'c') {
        printf("Let's continue then!\n");
    } else {
        if(check1 == 'r') {
            printf("\n");
            main();
        } else {
            return 0;
        }
    }

    switch(code) {
    case 1:
        printf("Your product came from the South");
        break;

    case 2:
        printf("Your product came from the North");
        break;

    case 3:
        printf("Your product came from the East");
        break;

    case 4:
        printf("Your product came from the West");
        break;

    default:
        if(code == 5 || code == 6 || (code >= 25 && code <= 30)) {
            printf("Your product came from the Northeast");
        } else {
            if(code >= 7 && code <= 9) {
                printf("Your product came from the Southeast");
            } else {
                if(code >= 10 && code <= 20) {
                    printf("Your product came from the Midwest");
                } else {
                    printf("Sorry, this code doesn't match with any correspondence");
                    return 0;
                }
            }
        }
        break;
    }

    printf(" and it costs %0.2f\n", price);
}

问题是:使用此代码,无论输入值是多少,输出始终为“对不起,此代码与任何对应关系均不匹配”。为什么会发生?我该如何解决?

(还对不起4个英语不好的人,我还在学习中)

1 个答案:

答案 0 :(得分:3)

scanf("%s", &check1);

您不能使用字符串格式说明符("%s")来扫描char,切换到

scanf("%c", &check1);

或更好

scanf(" %c", &check1); // The leading whitespace will consume the previous newline