首页 > 解决方案 > Clang-Tidy: 'scanf' 用于将字符串转换为整数值,但函数不会报告转换错误;考虑改用“strtol”(C)

问题描述

int SchedulingMethodSubMenu(struct linkedList process[], int quantumTime, FILE *fp,FILE *fr,char *ovalue,char line[LINE_MAX])
{
    printf("1)Go Back to Home \n");
    printf("2)Exit \n");
    printf("Option >");

    int choice;
    scanf("%d", &choice);
    while (choice != 1 && choice != 2)
    {
        scanf("%d", &choice);
    }
    if (choice == 1)
    {
        system("clear");
        SchedulingMethodMenu(process, quantumTime, fp, fr, ovalue, line);
    } else if (choice == 2)
    {
        system("clear");
        exit(0);
    }
    return 0;
}

大家好,

我在我的 C 项目中收到此错误

Clang-Tidy: 'scanf' 用于将字符串转换为整数值,但函数不会报告转换错误;考虑改用“strtol”(C++)

如果你没有得到错误的原因,你能帮助我吗?

标签: c

解决方案


这不是一条错误消息,而是来自 CLion 的警告,正如@WilliamPursell 所建议的那样,请改用 getchar 。

int SchedulingMethodSubMenu(struct linkedList process[], int quantumTime, FILE *fp,FILE *fr,char *ovalue,char line[LINE_MAX])
{
    printf("1)Go Back to Home \n");
    printf("2)Exit \n");
    printf("Option >");

    int choice;
    choice = getchar();
    while (choice != 1 && choice != 2)
    {
        choice = getchar();
    }
    if (choice == 1)
    {
        system("clear");
        SchedulingMethodMenu(process, quantumTime, fp, fr, ovalue, line);
    } else if (choice == 2)
    {
        system("clear");
        exit(0);
    }
    return 0;
}

其他堆栈溢出问题

有关的


推荐阅读