结构体B2无法录入书名而是直接从作者开始录入
#include<stdio.h>struct Date{
int year;
int month;
int day;
};
struct Book//结构体名称不是变量名
{
char title;
char author;
float price;
struct Date date;//结构体的嵌套
//unsigned int date;
char publisher;
};//结构体声明并不会分配空间只有当你定义一个新的结构体时才会分配空间
struct Book getInput(struct Book book){
printf("请输入书名\n");
scanf("%s",book.title);
printf("请输入作者\n");
scanf("%s",book.author);
printf("请输入售价\n");
scanf("%f",&book.price);
printf("请输入出版日期\n");
scanf("%d",&book.date.year,&book.date.month,&book.date.day);
printf("请输入出版社\n");
scanf("%d",book.publisher);
return book;
}
void output(struct Book book){
printf("书名%s\n",book.title);
printf("作者%s\n",book.author);
printf("售价%2f\n",book.price);
printf("出版日期:%d-%d-%d\n",book.date.year,book.date.month,book.date.day);
printf("出版社名称%d\n",book.publisher);
}
int main(void)
{
struct Book b1,b2;
printf("请录入第一本书的信息\n");
b1 = getInput(b1);
putchar('\n');
printf("请录入第二本书的信息\n");
b2=getInput(b2);
printf("\n\n信息录入完毕,现在开始打印\n");
output(b1);
output(b2);
本帖最后由 jkluoling1992 于 2020-4-19 12:59 编辑
struct Date{
int year;
int month;
int day;
};
struct Book//结构体名称不是变量名
{
char title;
char author;
float price;
struct Date date;//结构体的嵌套
//unsigned int date;
char publisher;
};//结构体声明并不会分配空间只有当你定义一个新的结构体时才会分配空间
struct Book getInput(struct Book book){
printf("请输入书名\n");
scanf("%s",book.title);
printf("请输入作者\n");
scanf("%s",book.author);
printf("请输入售价\n");
scanf("%f",&book.price);
printf("请输入出版日期\n");
scanf("%d%d%d",&book.date.year,&book.date.month,&book.date.day);
printf("请输入出版社\n");
scanf("%s",book.publisher);
return book;
}
void output(struct Book book){
printf("书名%s\n",book.title);
printf("作者%s\n",book.author);
printf("售价%2f\n",book.price);
printf("出版日期:%d-%d-%d\n",book.date.year,book.date.month,book.date.day);
printf("出版社名称%s\n",book.publisher);
}
标红的地方,格式写的有问题,还有年月日,要写三个%d
函数getInput 写的又问题,导致b2 输出的时候有些信息缺失
页:
[1]