简体   繁体   English

三个 scanf 导致两个并取垃圾值

[英]three scanf resulting in two and taking garbage value

Here Is code of Basic Structure , But the output is not as expected.这是基本结构的代码,但输出不符合预期。 There is three scanf function but only two is being executed.有三个 scanf 函数,但只有两个正在执行。 Middle one contains garbage value.中间一个包含垃圾值。

#include <stdio.h>

int main()
{
    struct book
    {
        char name;
        float price;
        int pages;
    };

    struct book b1,b2,b3;

    printf("Enter names , prices & no of pages of 3 books\n");
    scanf("%c%f%d",&b1.name,&b1.price,&b1.pages);
    scanf("%c%f%d",&b2.name,&b2.price,&b2.pages);
    scanf("%c%f%d",&b3.name,&b3.price,&b3.pages);

    printf("And this is what you entered\n");
    printf("%c%f%d",b1.name,b1.price,b1.pages);
    printf("%c%f%d",b2.name,b2.price,b2.pages);
    printf("%c%f%d",b3.name,b3.price,b3.pages);

    return 0;
}

Simply change简单地改变

scanf("%c%f%d", &bx.name, &bx.price, &bx.pages);

To

scanf(" %c%f%d", &b1.name, &b1.price, &b1.pages);

After you press Enter , a '\\n' is left in stdin , which will later be consumed by "%c".Enter 后, '\\n' 会留在stdin ,稍后将被 "%c" 使用。 Having read a character('\\n'), scanf() expects a floating-point number, as "%f" in the format string indicates.读取一个字符 ('\\n') 后, scanf()需要一个浮点数,如格式字符串中的“%f”所示。 However, instead of getting that desired floating-point number, it meets a character, and it returns sadly.然而,它并没有获得所需的浮点数,而是遇到了一个字符,并遗憾地返回。 As a result, &bx.price and &bx.pages are not updated, so they remain uninitialised, giving you the garbage values.结果, &bx.price&bx.pages不会更新,因此它们保持未初始化状态,为您提供垃圾值。

With a leading space in scanf() , all whitespace characters(if any) are discarded before reading starts.scanf()使用前导空格,在开始读取之前会丢弃所有空白字符(如果有)。 Since \\n is discarded, following reading process will(presumably) be successful.由于\\n被丢弃,以下读取过程将(大概)成功。

Also, just a tip: Always check the return value of scanf() , because you'll never know that stuff users will input.另外,只是一个提示:始终检查scanf()的返回值,因为您永远不会知道用户将输入的内容。

Example code:示例代码:

#include <stdio.h>

struct book
{
    char name;
    float price;
    int pages;
};

int main()
{
    struct book b1, b2, ..., bx;

    printf("Enter names, prices & no of pages of x books:\n");
    while (scanf(" %c%f%d", &bx.name, &bx.price, &bx.pages) != 3)
    { 
        fputs("Error reading bx. Please try again:\n", stderr);
        scanf("%*[^\n] ");
    }
    ......

    printf("And this is what you have entered:\n");
    printf("%c %f %d", bx.name, bx.price, bx.pages);
    ......

    return 0;
}

Example input & output:示例输入和输出:

Enter names, prices & no of pages of x books:
asd wedewc efcew
Error reading bx. Please try again:
a 12.34 42
And this is what you have entered:
a 12.340000 42

scanf, usually, reads the characters from standard input stream, which includes your newlines as well as spaces. scanf 通常从标准输入流中读取字符,其中包括换行符和空格。 so try to flush all unwanted inputs left in the stdin stream before you use scanf("%c");因此,在使用 scanf("%c"); 之前,请尝试刷新 stdin 流中留下的所有不需要的输入;

Also remember: do not let %c fall somewhere in the middle of the format string还要记住:不要让 %c 落在格式字符串的中间

Hope this could help.希望这会有所帮助。 unfortunately you should use it before all scanf in "Your" program不幸的是,您应该在“您的”程序中的所有 scanf 之前使用它

printf("Enter names , prices & no of pages of 3 books\n");
fflush(stdin);
scanf("%c%f%d",&b1.name,&b1.price,&b1.pages);

You may also use a space before %c in scanf to read whitespaces.您也可以在 scanf 中使用 %c 之前的空格来读取空格。

scanf(" %c"....);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM