C 编程 - 调用 fgets() 两次?

发布于 2025-01-08 18:33:09 字数 1807 浏览 3 评论 0原文

在我的 C 程序中,我调用 fgets() 两次来获取用户的输入。但是,在第二次调用 fgets() (在函数中)时,它不会等待输入,它只是跳过它,就好像它甚至没有询问一样为了它。这是我的代码(稍微缩短了一点):

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

#define ARE_EQUAL 0

void rm_nl(char *c, int s);
float ctof();
float ftoc();

int main()
{   
    char str[2];                                     // Setting vars
    float result;

    printf("Type 'C' or 'F': ");                     // Prompt

    fgets(str, 2, stdin);                            // <-- First fgets
    rm_nl(str, 2);                                   // rm_nl() removes the newline 
                                                     // from input
    printf("\n");

    if(strcmp(str, "C") == ARE_EQUAL || strcmp(str, "c") == ARE_EQUAL)
    {
        result = ctof();                         // Compares strings and calls
        printf("%.2f\n", result);                // function conditionally
    }
    else
    {
        result = ftoc();
        printf("%.2f\n", result);
    }

    return 0;
}

float ctof()                                          // One of the two functions
{                                                     // (they are almost the same) 
    char input[64];
    float fahr, cels;                                 // Local vars

    printf("Type in a Celsius value: ");             // Prompt

    fgets(input, 64, stdin);                               // <-- Second fgets
    rm_nl(input, sizeof(input));

        // Yadda yadda yadda
}


// Second function and rm_nl() not shown for readability

这个程序会输出类似这样的内容:

Type 'C' or 'F': (value)

然后...

Type a Celsius value: 57.40 (I don't type this)

(Program terminates)

它甚至不需要我就填写了 57.40打字吧!我应该采取什么不同的做法?

In my C program, I call fgets() twice to get input from the user. However, on the second call of fgets() (which is in a function), it doesn't wait for the input to be taken, it just skips over it as if it didn't even ask for it. Here is my code (shortened down a bit):

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

#define ARE_EQUAL 0

void rm_nl(char *c, int s);
float ctof();
float ftoc();

int main()
{   
    char str[2];                                     // Setting vars
    float result;

    printf("Type 'C' or 'F': ");                     // Prompt

    fgets(str, 2, stdin);                            // <-- First fgets
    rm_nl(str, 2);                                   // rm_nl() removes the newline 
                                                     // from input
    printf("\n");

    if(strcmp(str, "C") == ARE_EQUAL || strcmp(str, "c") == ARE_EQUAL)
    {
        result = ctof();                         // Compares strings and calls
        printf("%.2f\n", result);                // function conditionally
    }
    else
    {
        result = ftoc();
        printf("%.2f\n", result);
    }

    return 0;
}

float ctof()                                          // One of the two functions
{                                                     // (they are almost the same) 
    char input[64];
    float fahr, cels;                                 // Local vars

    printf("Type in a Celsius value: ");             // Prompt

    fgets(input, 64, stdin);                               // <-- Second fgets
    rm_nl(input, sizeof(input));

        // Yadda yadda yadda
}


// Second function and rm_nl() not shown for readability

This program would output something like:

Type 'C' or 'F': (value)

and then...

Type a Celsius value: 57.40 (I don't type this)

(Program terminates)

It fills in the 57.40 without me even typing it! What should I do differently?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈下烟灰 2025-01-15 18:33:09
fgets(str, 2, stdin);

您为 fgets 提供的空间太少。您只允许它读取一个字符(因为 2 包含 0 终止符)。

换行符将始终保留在输入缓冲区中,以便下一个 stdio 操作将读取它。

fgets(str, 2, stdin);

You're providing too little space for fgets. You only allow it to read one character (since 2 includes the 0-terminator).

The newline will always be left in the input buffer so the next stdio operation will read it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文