【问题标题】:Confusion with scanf() or if()与 scanf() 或 if() 混淆
【发布时间】:2019-10-17 11:57:45
【问题描述】:

我对C不是很熟悉。因此,也许有人会很容易找到解决方案,如果您分享,我不介意。 在第一个 scanf() 中输入数据后,总是给出选项 else(): "Error"。

我一直在寻找解决问题的可能方案。我发现了很多类似的东西,但没有什么能特别帮助我。我认为错误出在 strcmp() 中。但我不能肯定地说。你会帮忙吗?

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

int main()
{
        float celsius, fahrenheit;
        char tempConvering[10];

        printf("Enter what to convert to what (F to C; C to F): ");
        scanf(" %s", &tempConvering[10]);

        if(strcmp(tempConvering, "F to C") == 0)
        {
            printf("\nEnter temperature in Fahrenheit: ");
            scanf(" %f", &fahrenheit);
            celsius = fahrenheit * 1.8 + 32;
            printf("%.2f Fahrenheits = %.2f Celsius\n", fahrenheit, celsius);
        }
        else if(strcmp(tempConvering, "C to F") == 0)
        {
            printf("\nEnter temperature in Celsius: ");
            scanf(" %f", &celsius);
            celsius = (fahrenheit - 32) / 1.8;
            printf("%.2f Celsius = %.2f Fahrenheits\n", celsius, fahrenheit);
        }
        else
        {
            puts("\nError!");
        }
}

【问题讨论】:

  • scanf("%s") 停在空格处,因此输入“F to C\n”被部分读取为“F”,其余部分留在输入缓冲区中。试试fgets()(记住fgets() 也读换行符)。
  • 我的建议:永远不要使用scanf
  • &amp;tempcovering[10] 地址超出数组末尾。就做scanf(" %9s", tempConvering);

标签: c string if-statement scanf strcmp


【解决方案1】:

在执行scanf() 函数时不要放置额外的空间。

这有时可能是个问题!

这应该可以正常工作:

scanf("%s", tempConvering);

顺便说一句,当您将字符串作为输入时,无需在字符串名称前使用&amp;

上面已经有人描述过了。

【讨论】:

  • 小心 - 将 tempCovering[10] 作为最后一个参数传递给 scanf 可能会导致段错误,因为程序试图将参数解释为指针。
  • 哦,是的,我的错!我一定很赶时间!
【解决方案2】:
#include <stdio.h>
#include <string.h>

int main()
{
        float celsius, fahrenheit;
        char tempConvering[20];

        printf("what do you want to convert? ");
        scanf("%s", tempConvering);

        if(strcmp(tempConvering, "Fahrenheits") == 0)
        {
            printf("Enter temperature in Fahrenheit: ");
            scanf("%f", &fahrenheit);
            celsius = (fahrenheit - 32) / 1.8;
            printf("%.2f Fahrenheits = %.2f Celsius\n", fahrenheit, celsius);
        }
        else if(strcmp(tempConvering, "Celsius") == 0)
        {
            printf("Enter temperature in Celsius: ");
            scanf("%f", &celsius);
            fahrenheit = celsius * 9 / 5 + 32;
            printf("%.2f Celsius = %.2f Fahrenheits\n", celsius, fahrenheit);
        }
        else
        {
            puts("\nError!");
        }
}

这就是答案。我必须感谢您的提示,我会尽量记住有关 scanf() 的所有细节。但是,只有当我将所需的答案更改为“F 到 C”而不是“华氏度”时,问题才消失。好吧,我分别改变了问题。该计划立即获得。然而,尝试使用 scanf() 做某事是不成功的,因为 fgets() 会发生同样的事情。

无论如何,问题总算解决了,谢谢大家!

【讨论】:

    【解决方案3】:

    我认为您使用 scanf 的方式存在错误,具体而言,此时:

    scanf(" %s", &tempConvering[10]);
                      ^
                      |
                      +---- here
    

    scanf 的第二个参数应该是存储结果的地址。在这里,您说的是“将读入的字符串放在内存中我设置的缓冲区之后”,这可能不是您想要做的。而是这样写:

    scanf(" %s", tempConvering);
    

    这表示“将字符串放在名为tempConverting 的缓冲区中”。如果您刚刚开始使用 C 并且对指针和数组了解不多,那么一个好的经验法则是,如果您使用 `scanf 读取字符串,您应该只给出您所在的数组变量的名称想要存储字符串而不是使用 & 符号。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2011-11-08
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 2014-02-03
      • 1970-01-01
      相关资源
      最近更新 更多