Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中循环中的scanf只询问用户一次_C_Loops_Scanf - Fatal编程技术网

C中循环中的scanf只询问用户一次

C中循环中的scanf只询问用户一次,c,loops,scanf,C,Loops,Scanf,我正在尝试创建一个简单的转换器,它将要求用户输入n次数据。理想情况下,循环中的scanf应该允许用户在终端中输入响应,按enter键,然后传递输出,然后再次询问…n次。但到目前为止,该程序只要求一次,从不允许用户再次要求 我见过很多关于scanf的循环,但我无法将我的问题与它们中的任何一个联系起来 我是来自java的C新手 #include <stdio.h> double feet(double m); double lbs(double g); double f(double c

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

我见过很多关于scanf的循环,但我无法将我的问题与它们中的任何一个联系起来

我是来自java的C新手

#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是如何工作的。谢谢大家!

这里有几个问题

您将void*类型的NULL赋值到一个char中,并在以后比较这些值。不要这样做。如果要使用某种金丝雀值,可以改为使用字符“\0”或任何其他不易输入的字符。 您已经为scanf提供了一个参数,它希望将char*作为char。为了行扫描%lf%c\n,输入(&I),单位;要按预期工作,您应该在单元前面有一个符号AND,以便将指针传递到局部变量单元。 为scanf提供尾随空格要求它读取所有后续空格,直到它确定一个空格块已结束为止。有关更多信息,请参见man 3 scanf,但请注意,格式字符串中的任何空格字符都将被同等对待。在这种情况下,在scanf调用的末尾有一个换行符将要求它们读取一定量的空格,然后读取一个非空格字符。另请参见此处的接受答案:。只需关闭\n。 一些大括号(即函数main的块)未闭合。我假设这只是一个复制和粘贴的功能。 使用更严格的编译命令可以避免大部分这种情况。如果您将gcc与C99标准一起使用,您可以尝试
gcc-Wall-Werror-Wextra-Wshadow-pedantic-std=c99 source_file.c.

首先,我建议使用_s函数而不是常规函数,例如printf_s和not printf。其次,我写下了一个有效的代码版本。我注意到,每当我向scanf_s添加一些文本时,例如a\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;
}

注意,NULL在技术上是一个指针,因此赋值字符单位=NULL;这是坏习惯。您想要的是char unit='\0';因为“\0”是NUL字符。不需要围绕scanf的if语句。相反,您需要一个if语句来验证scanf是否返回了2,例如,if scanf%lf%c、&input、&unit!=2休息;谢谢大家-->这个问题解决了ifscanf%lf%c\n、输入和单位!=2休息```