【发布时间】:2021-09-07 08:10:18
【问题描述】:
感谢之前的帮助
现在我面临输出问题 当我在控制台中输入时,它会自动换行 \n 从我附加的屏幕截图中可以看出
请指出问题
PS:如果有人能告诉我什么是“stdin”,我会非常感激 注意:我刚刚更新了代码,请看一下
#include <stdio.h>
#include <string.h>
void input();
void output();
struct book
{
char title[70],id[70],aname[70],price[5];
}b1,b2;
void main()
{
input();
output();
}
void input()
{
int i;
char t[70],in[70],p[5],an[70];
for(i=1;i<3;++i)
{
printf("type the ID for book %d:",i);
fgets(in,70,stdin);
printf("type the title for book %d:",i);
fgets(t,70,stdin);
printf("type the author name for book %d:",i);
fgets(an,70,stdin);
printf("type the price for book %d:",i);
fgets(p,5,stdin);
printf("\n");
if(i==1)
{
strcpy(b1.id,in);
strcpy(b1.title,t);
strcpy(b1.aname,an);
strcpy(b1.price,p);
}
else if(i==2)
{
strcpy(b2.id,in);
strcpy(b2.title,t);
strcpy(b2.aname,an);
strcpy(b2.price,p);
}
}
}
void output()
{
printf("Sr.No.\t\tID\t\tTITLE\t\tAUTHOR NAME\t\tPRICE\n");
for(int i=1;i<=2;i++)
{
if(i==1)
{
printf("%d\t\t%s\t\t%s\t\t%s\t\t%s\t\t",i,b1.id,b1.title,b1.aname,b1.price);
printf("\n");
}
if(i==2)
{
printf("%d\t\t%s\t\t%s\t\t%s\t\t%s\t\t",i,b2.id,b2.title,b2.aname,b2.price);
printf("\n");
}
}
}
【问题讨论】:
-
您正在调用函数 input() 和 output() 而不指定参数;.
-
几乎可以肯定,
gets(p);会造成破坏。 -
如果您输入超过 4 个字符的价格,所有的地狱都会崩溃。
gets()不知道你只分配了char p[5],你也没办法说出来。这就是为什么gets()已从语言中删除的原因,因为无法安全地使用它。 -
请求更新程序的帮助
-
如果有人能告诉我标准输入是什么,我会很高兴
标签: c function struct structure