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

问题描述 投票:1回答:2

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

我见过很多关于scanf in loop的posts,但是我无法将问题与其中任何一个联系起来。

我是来自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的工作原理。谢谢!

c loops scanf
2个回答
3
投票

您在这里有几个问题。

  1. 您正在将NULL类型的void*分配到char中,并在以后比较这些值。不要这样如果要使用某种类型的canary值,则可以改用字符'\0'(或任何其他不易输入的字符)。
  2. 您已经给scanf提供了一个参数,该参数期望是char*作为char。为了使行scanf("%lf %c\n", &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


2
投票

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

FYI:对于初始化,您需要输入用户不会输入的数字-如果用户未输入值,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;
}
© www.soinside.com 2019 - 2024. All rights reserved.