错误的输出显示使用结构(Wrong output display With Structures)

编程入门 行业动态 更新时间:2024-10-26 18:18:51
错误的输出显示使用结构(Wrong output display With Structures)

我试图在C程序中执行以下结构,但它不起作用。 它不能正确打印给定值。

#include<stdio.h> #include<conio.h> int main() { struct book { char name; int page; float price; }; struct book b1, b2, b3; clrscr(); printf("\n 1st data \n"); scanf("%c%f%d",&b1.name,&b1.price,&b1.page); printf("\n 2nd data"); scanf(" %c%f%d",&b2.name,&b2.price,&b2.page); printf("\n3rd data"); scanf("%c%f%d",&b3.name,&b3.price,&b3.page); printf("%c%f%d",b1.name,b1.price,b1.page); printf("%c%f%d",b2.name,b2.price,b2.page); printf("%c%f%d",b3.name,b3.price,b3.page); getch(); return 0; }

每次我运行上面的代码,我得到不同的输出..其中一个是:: ....输出....

data p 1 2.2 s 2 3.3 p0.1000002.1.00000022015s-?.<00000000000000000000000000000e+15315872

I 'm trying to execute the following structure in a C program but it's not working. It does not print the given values correctly.

#include<stdio.h> #include<conio.h> int main() { struct book { char name; int page; float price; }; struct book b1, b2, b3; clrscr(); printf("\n 1st data \n"); scanf("%c%f%d",&b1.name,&b1.price,&b1.page); printf("\n 2nd data"); scanf(" %c%f%d",&b2.name,&b2.price,&b2.page); printf("\n3rd data"); scanf("%c%f%d",&b3.name,&b3.price,&b3.page); printf("%c%f%d",b1.name,b1.price,b1.page); printf("%c%f%d",b2.name,b2.price,b2.page); printf("%c%f%d",b3.name,b3.price,b3.page); getch(); return 0; }

Every time I run the above code, I get different output.. one of them is:: ....output....

data p 1 2.2 s 2 3.3 p0.1000002.1.00000022015s-?.<00000000000000000000000000000e+15315872

最满意答案

在给出第二个输入之后,输入缓冲区可能会变满,因此它不接受第3个输入。 每次输入后尝试使用fflush(stdin)。

After giving the second input , the input buffers might get full and hence its not accepting the 3rd input . try using fflush(stdin) after every input .

更多推荐

本文发布于:2023-07-16 12:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128530.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   结构   Wrong   Structures   display

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!