无法在此 C 程序中找到错误

发布于 2024-09-12 10:38:28 字数 922 浏览 4 评论 0原文

我无法在这个 C 程序中找到错误。

#include <stdio.h>

int main()
{ 

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

    struct book b[3] ;
    int i ; int k;
    for ( i = 0 ; i <= 2 ; i++ )
    { 
        printf ( "\nEnter name, price and pages: " ) ;
        k = scanf ( "%c %f %d", &b[i].name, &b[i].price, &b[i].pages ) ;
    } 
    for ( i = 0 ; i <= 2 ; i++ ) 
        printf ( "\n%c %f %d", b[i].name, b[i].price, b[i].pages ) ;
    //getch();
    return 0;
}

运行时:

Enter name, price and pages: a 1 1

Enter name, price and pages: b 2 2

Enter name, price and pages:
a 1.000000 1

 7922540190797673100000000000000000.000000 4200368
b 2.000000 2

我想为每个 scanfs 提供 a 1 1b 2 2c 3 3 作为我的输入,但它没有等待我在第三个 scanf 中的输入。为什么会这样呢?为什么它读取我第二次输入到数组的第三个元素?

I'm unable to find bug in this C program.

#include <stdio.h>

int main()
{ 

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

    struct book b[3] ;
    int i ; int k;
    for ( i = 0 ; i <= 2 ; i++ )
    { 
        printf ( "\nEnter name, price and pages: " ) ;
        k = scanf ( "%c %f %d", &b[i].name, &b[i].price, &b[i].pages ) ;
    } 
    for ( i = 0 ; i <= 2 ; i++ ) 
        printf ( "\n%c %f %d", b[i].name, b[i].price, b[i].pages ) ;
    //getch();
    return 0;
}

run time:

Enter name, price and pages: a 1 1

Enter name, price and pages: b 2 2

Enter name, price and pages:
a 1.000000 1

 7922540190797673100000000000000000.000000 4200368
b 2.000000 2

I wanted to give a 1 1 , b 2 2, c 3 3 as my inputs for each scanfs but it didn't wait for my input in 3rd scanf. Why so? and why did it read my 2nd time input into 3rd elementof array?

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

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

发布评论

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

评论(3

牵强ㄟ 2024-09-19 10:38:28

在您的代码后添加 getchar() scanf()

 for ( i = 0 ; i <= 2 ; i++ )
  { 
      printf ( "\nEnter name, price and pages: " ) ;
      k = scanf ( "%c %f %d", &b[i].name, &b[i].price, &b[i].pages ) ;

      getchar(); //will clear the buffer
  } 

PS:不要使用scanf() 用于 char 条目。

Add a getchar() after your scanf()

 for ( i = 0 ; i <= 2 ; i++ )
  { 
      printf ( "\nEnter name, price and pages: " ) ;
      k = scanf ( "%c %f %d", &b[i].name, &b[i].price, &b[i].pages ) ;

      getchar(); //will clear the buffer
  } 

P.S: Don't use scanf() for char entry.

指尖微凉心微凉 2024-09-19 10:38:28

与其他说明符不同,%c 与 scanf 一起使用时不会忽略空格。在任何情况下,您可能都希望将名称字段设置为字符串:

#include <stdio.h>

int main()
{ 
    struct book 
    { 
        char name[10] ;   // or some suitable size
        float price ; 
        int pages ; 
    } ;

    struct book b[3] ;
    int i ; int k;
    for ( i = 0 ; i <= 2 ; i++ )
    { 
        printf ( "\nEnter name, price and pages: " ) ;
        k = scanf ( "%s %f %d", b[i].name, &b[i].price, &b[i].pages ) ;
    } 
    for ( i = 0 ; i <= 2 ; i++ ) 
        printf ( "\n%s %f %d", b[i].name, b[i].price, b[i].pages ) ;
    return 0;
}

Unlike the other specifiers, %c when used with scanf does not ignore whitespace. You probably want to make the name fields strings in any case:

#include <stdio.h>

int main()
{ 
    struct book 
    { 
        char name[10] ;   // or some suitable size
        float price ; 
        int pages ; 
    } ;

    struct book b[3] ;
    int i ; int k;
    for ( i = 0 ; i <= 2 ; i++ )
    { 
        printf ( "\nEnter name, price and pages: " ) ;
        k = scanf ( "%s %f %d", b[i].name, &b[i].price, &b[i].pages ) ;
    } 
    for ( i = 0 ; i <= 2 ; i++ ) 
        printf ( "\n%s %f %d", b[i].name, b[i].price, b[i].pages ) ;
    return 0;
}
幻梦 2024-09-19 10:38:28

您可以在 scanf 之前添加 fflush(stdin);

事实证明,您不应该在 stdin 上使用 fflush,因为 fflush 定义为在输出流上工作。换句话说,fflush 在输入流上有未定义的行为。

int fflush(FILE *ostream);

C 标准的摘录说:

ostream 指向输出流
一个更新流,其中最
未输入最近操作,则
fflush 函数会导致任何未写入的
要传送的流的数据
到要写入的主机环境
到文件; 否则,该行为
未定义。

You can add fflush(stdin); before your scanf..

It turns out that you should not use fflush on stdin because fflush is only defined to work on output streams. In other words fflush has undefined behaviour on input streams.

int fflush(FILE *ostream);

an extract from the C standard says:

ostream points to an output stream or
an update stream in which the most
recent operation was not input, the
fflush function causes any unwritten
data for that stream to be delivered
to the host environment to be written
to the file; otherwise, the behavior
is undefined.

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