有人能告诉我为什么进入case 1被完全忽略了吗?

    // #define M 50 is at the top

    char product[M] = {0};
    int choice = -1;

    printf("Command: ");
    scanf("%d", &choice);

    switch(choice){

        case 0:
            break;

        case 1:
            printf("Product: ");
            gets(product);
            insert_product(warehouse, price, product);
            break;

        case 2:
            // print_all();
            break;

        default:
            printf("Scelta non valida\n");
            break;

    }

最佳答案

获取可能被前一个输入的结束字符跳过。在获取之前尝试添加getchar(),看看这是否是问题所在。

关于c - 开关内部的gets()被忽略,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10840740/

10-10 21:24
查看更多