【问题标题】:scanf in a loop in C only asks user onceC语言循环中的scanf只询问用户一次
【发布时间】:2019-12-25 16:40:53
【问题描述】:

我正在尝试创建一个简单的转换器,它会要求用户输入数据 n 次。理想情况下,循环中的scanf() 应允许用户在终端中输入响应,按回车键,然后传递输出,然后再次询问....n 次。但是到目前为止,该程序只询问一次,并且不允许用户再次询问。

在我看到的循环中有很多关于scanfposts,但我无法将我的问题与其中任何一个联系起来。

我是 C 新手,来自 Java。

#include <stdio.h>
double feet(double m);
double lbs(double g);
double f(double c);
//double askInput(int num);


int main()
{
    int num, i;
    i = 0;
    
    printf("how many?\n");
    scanf("%i\n", &num);
    
    for(i = 0; i < num; i++)
    {
    double input = 0.0;
    double output;
    char unit = NULL;
        printf("num to convert, units to convert?\n");
        
        //scanf("%lf %c\n", &input, unit);
        if(input == 0.0 && unit == NULL)
        {
            //input = askInput(num);
            scanf("%lf %c\n", &input, unit);
        }
        
        //meters to feet
        if(unit == 'm')
        {
            output = feet(input);
        }
        
    }

我尝试了很多方法。我试过使用while循环,我试过把scanf放在一个单独的函数中,然后也使用if语句。我想我不太了解scanf 的工作原理。

【问题讨论】:

  • 请注意,NULL 在技术上是一个指针,因此分配 char unit = NULL; 是不好的做法。你想要的是char unit = '\0';,因为'\0' 是NUL 字符。并且不需要围绕scanfif 语句。相反,您需要一个 if 语句来验证 scanf 返回 2,例如if (scanf("%lf %c", &amp;input, &amp;unit) != 2) break;

标签: c loops scanf


【解决方案1】:

这里有几个问题。

  1. 您将NULLvoid* 类型)分配给char,并在稍后比较这些值。不要这样做。如果您想使用某种金丝雀值,您可以改用字符 '\0'(或任何其他不易输入的字符)。
  2. 您已为 scanf 提供了一个参数,它预计将是 char* 作为 char。为了使scanf("%lf %c\n", &amp;input, unit); 行按预期工作,您应该在unit 前面有一个与号,以便将指针传递给局部变量unit
  3. 提供scanf 尾随空格要求它读取所有后续空格,直到它可以确定一个空格块已结束(有关更多信息,请参阅man 3 scanf,但请注意,格式字符串被同等对待)。在这种情况下,在您的 scanf 调用末尾有一个换行符将要求他们读取一些空格,然后是一个非空格字符(另请参见此处接受的答案:white space in format string of scanf())。不用\n
  4. 您的一些大括号(即函数 main 的块)没有关闭。我假设这只是复制粘贴到 SO 中的功能。

使用更严格的编译命令可以避免大部分情况。如果您将 gcc 与 C99 标准一起使用,您可以尝试 gcc -Wall -Werror -Wextra -Wshadow -pedantic -std=c99 source_file.c.

【讨论】:

  • 谢谢大家--->这解决了! ``` if(scanf("%lf %c\n", &input, &unit) != 2) break;```
【解决方案2】:

首先,我建议使用 _s 函数而不是常规函数(例如 printf_s 而不是 printf)。其次,我写下了一个有效的代码版本。我注意到,每当我向 scanf_s(例如 \n)添加一些文本时,程序并没有停止,但是当我使用 printf 打印它时,程序停止了。

仅供参考:对于初始化,您需要输入一个用户不会输入的数字 - 如果用户没有输入值,num 不会存储 0,而是 -9.2559631349317831e+61。你应该使用 ||而不是 && 因为您希望 num 和 unit 将存储一个值! :)

这是我的代码:

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

int main()
{
    int amount;
    printf_s("How many numbers would you like to recive?\n");
    scanf_s("%i", &amount);

    for (int i = 0; i < amount; ++i)
    {

        // Initialization of all variables.
        double convertedNum;
        double rawNum;
        char unit[3] = "";

        // Getting the variables.
        printf_s("Please enter the number that you\'d like to convert and the unit to 
        convert to.\n");
        printf_s("Number: ");
        scanf_s("%lf", &rawNum);
        printf_s("Units (m/ft): ");
        scanf_s("%s", &unit, 3);

        // To make sure that we're getting right input & the wanted amount of 
        // numbers.

        if (rawNum == -9.2559631349317831e+61 || unit == "")
        {
            printf_s("Please enter a number and a unit!");
            --i;
        }

        else if (strcmp(unit, "m") == 0)
        {
            convertedNum = rawNum * 3.2808399;
            printf_s("\n\nAfter convention, the number is %f\n", convertedNum);
        }

        else if (strcmp(unit, "ft") == 0)
        {
            convertedNum = rawNum / 3.2808399;
            printf_s("\n\nAfter convention, the number is %f\n", convertedNum);
        }
    }

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    相关资源
    最近更新 更多