You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言中fgets输入字符串后strcmp匹配失败的问题求助

问题:单位转换器Distance函数匹配失败,始终提示无效单位

我正在开发一款单位转换器,编写了Distance()、time()等不同类型的转换函数。在Distance函数中,我使用fgets()获取cm、mm等多字符单位输入,期望通过strcmp()匹配不同的单位组合来执行对应的转换计算,但程序始终跳转到提示“Please enter valid conversion units”,无法执行正确的计算逻辑。我是编程初学者,可能是细节错误,请求帮助。以下是我的代码:

int Distance() {
    char from[40], to[40];
    double val;
    double result;

    printf("Enter what you want to convert from: ");
    fflush(stdin);
    fgets(from, 40, stdin);

    printf("Enter what you want to convert to: \n");
    fflush(stdin);
    fgets(to, 40, stdin);

    printf("Enter the numerical value of conversion: \n");
    scanf("%lf", &val);

    (strcmp(from, "cm") == 0 && strcmp(to, "mm") == 0) ? printf("Answer is %lf MilliMeters", val / 10) : 
    (strcmp(from, "mm") == 0 && strcmp(to, "cm") == 0) ? printf("Answer is %lf CentiMeters", val * 10) :
    (strcmp(from, "cm") == 0 && strcmp(to, "km") == 0) ? printf("Answer if %lf KiloMeters", val * 100000) : 
    (strcmp(from, "km") == 0 && strcmp(to, "cm") == 0) ? printf("Answer is %lf CentiMeters", val / 100000) : 
    (strcmp(from, "mm") == 0 && strcmp(to, "km") == 0) ? printf("Anser is %lf KiloMeters", val  * 1000000) : 
    (strcmp(from, "km") == 0 && strcmp(to, "mm") == 0) ? printf("Answer is %lf  MilliMeters", val / 1000000) : 
    printf("Please enter valid conversion units");
}

解决方案

1. 核心问题:fgets()读取的字符串包含换行符

fgets()会把用户输入时按下的回车键(\n)也读取到字符串末尾,比如输入cm后按回车,from数组里实际存储的是"cm\n",而strcmp(from, "cm")对比的是不带换行的字符串,自然不相等,导致所有匹配条件失效。

修复方法:读取后去除字符串末尾的换行符:

// 处理from的换行符
size_t len = strlen(from);
if (len > 0 && from[len-1] == '\n') {
    from[len-1] = '\0';
}
// 同理处理to的换行符
len = strlen(to);
if (len > 0 && to[len-1] == '\n') {
    to[len-1] = '\0';
}

2. 禁用fflush(stdin)

fflush(stdin)是未定义行为,标准C仅允许对输出流使用fflush,对输入流使用会引发不可预测的问题。若要清除输入缓冲区残留字符,改用循环读取:

// 替代fflush(stdin)的正确写法
int c;
while ((c = getchar()) != '\n' && c != EOF);

3. 修正转换公式与拼写错误

代码里存在多处错误:

  • 单位转换公式写反:cm转mm应是val * 10(1cm=10mm),你写的是val / 10;cm转km应是val / 100000,你写的是val * 100000;mm转km应是val / 1000000,你写的是val * 1000000
  • 拼写错误:Answer if应为Answer is,Anser应为Answer

修正后的完整代码

#include <stdio.h>
#include <string.h>

int Distance() {
    char from[40], to[40];
    double val;

    printf("Enter what you want to convert from: ");
    // 清除输入缓冲区残留
    int c;
    while ((c = getchar()) != '\n' && c != EOF);
    fgets(from, 40, stdin);
    // 去掉换行符
    size_t len = strlen(from);
    if (len > 0 && from[len-1] == '\n') {
        from[len-1] = '\0';
    }

    printf("Enter what you want to convert to: ");
    while ((c = getchar()) != '\n' && c != EOF);
    fgets(to, 40, stdin);
    len = strlen(to);
    if (len > 0 && to[len-1] == '\n') {
        to[len-1] = '\0';
    }

    printf("Enter the numerical value of conversion: ");
    scanf("%lf", &val);

    if (strcmp(from, "cm") == 0 && strcmp(to, "mm") == 0) {
        printf("Answer is %.2lf MilliMeters\n", val * 10);
    } else if (strcmp(from, "mm") == 0 && strcmp(to, "cm") == 0) {
        printf("Answer is %.2lf CentiMeters\n", val / 10);
    } else if (strcmp(from, "cm") == 0 && strcmp(to, "km") == 0) {
        printf("Answer is %.6lf KiloMeters\n", val / 100000.0);
    } else if (strcmp(from, "km") == 0 && strcmp(to, "cm") == 0) {
        printf("Answer is %.0lf CentiMeters\n", val * 100000.0);
    } else if (strcmp(from, "mm") == 0 && strcmp(to, "km") == 0) {
        printf("Answer is %.9lf KiloMeters\n", val / 1000000.0);
    } else if (strcmp(from, "km") == 0 && strcmp(to, "mm") == 0) {
        printf("Answer is %.0lf MilliMeters\n", val * 1000000.0);
    } else {
        printf("Please enter valid conversion units\n");
    }
    return 0;
}

内容的提问来源于stack exchange,提问作者sumiicode.exe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 09:14:55