书本信息录入(结构体问题)
本帖最后由 焦糖橙子 于 2021-8-8 09:57 编辑建立一个图书馆,录入书本信息
我在录入售价的时候出了问题,能看看哪里写错了吗?{:10_266:}
#include<stdio.h>
#include<stdlib.h>
void get_imput(struct Book *library);//录入信息
void printBook(struct Book *library);//打印信息
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title;
char author;
float price;
struct Date date;
char publisher;
};
int main(void)
{
int n;
struct Book *library;
printf("请输入要录入的书本数量:");
scanf("%d",&n);
for(int i=0;i<n;i++)//分配内存空间
{
library=(struct Book*)malloc(sizeof(struct Book));
}
for(int i=0;i<n;i++)
{
printf("正在录入第 %d 本书的信息...\n",i+1);
get_imput(library);
}
printf("====已经全部录入,现在开始打印验证====\n");
for(int i=0;i<n;i++)
{
printf("打印第 %d 本书的信息...\n",i+1);
}
for(int i=0;i<n;i++)//释放内存空间
{
free(library);
}
return 0;
}
void get_imput(struct Book *library)
{
printf("请输入书名:");
scanf(" %s",library->title);
printf("请输入作者:");
scanf(" %s",library->author);
printf("请输入售价:");
scanf("%f",library->price);
printf("请输入出版日期:");
scanf("%d-%d-%d",library->date.year,library->date.month,library->date.day);
printf("请输入出版社:");
scanf(" %s",library->publisher);
}
void printBook(struct Book *library)
{
printf("书名:《%s》",library->title);
printf("作者:%s",library->author);
printf("售价:%.2f",library->price);
printf("出版日期:%d-%d-%d",library->date.year,library->date.month,library->date.day);
printf("出版社:%s",library->publisher);
}
scanf("%d-%d-%d",library->date.year,library->date.month,library->date.day); 你的编译器什么提示也没有?换一个好点的
$ gcc -g -Wall -o main main.c
main.c:4:23: warning: ‘struct Book’ declared inside parameter list will not be visible outside of this definition or declaration
4 | void get_imput(struct Book *library);//录入信息
| ^~~~
main.c:5:23: warning: ‘struct Book’ declared inside parameter list will not be visible outside of this definition or declaration
5 | void printBook(struct Book *library);//打印信息
| ^~~~
main.c: In function ‘main’:
main.c:41:26: warning: passing argument 1 of ‘get_imput’ from incompatible pointer type [-Wincompatible-pointer-types]
41 | get_imput(library);
| ~~~~~~~^~~
| |
| struct Book *
main.c:4:29: note: expected ‘struct Book *’ but argument is of type ‘struct Book *’
4 | void get_imput(struct Book *library);//录入信息
| ~~~~~~~~~~~~~^~~~~~~
main.c: At top level:
main.c:59:6: error: conflicting types for ‘get_imput’
59 | void get_imput(struct Book *library)
| ^~~~~~~~~
main.c:4:6: note: previous declaration of ‘get_imput’ was here
4 | void get_imput(struct Book *library);//录入信息
| ^~~~~~~~~
main.c: In function ‘get_imput’:
main.c:66:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
66 | scanf("%f",library->price);
| ~^~~~~~~~~~~~~~~
| | |
| float * double
main.c:68:13: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
68 | scanf("%d-%d-%d",library->date.year,library->date.month,library->date.day);
| ~^ ~~~~~~~~~~~~~~~~~~
| | |
| int * int
main.c:68:16: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat=]
68 | scanf("%d-%d-%d",library->date.year,library->date.month,library->date.day);
| ~^ ~~~~~~~~~~~~~~~~~~~
| | |
| int * int
main.c:68:19: warning: format ‘%d’ expects argument of type ‘int *’, but argument 4 has type ‘int’ [-Wformat=]
68 | scanf("%d-%d-%d",library->date.year,library->date.month,library->date.day);
| ~^ ~~~~~~~~~~~~~~~~~
| | |
| int * int
main.c: At top level:
main.c:73:6: error: conflicting types for ‘printBook’
73 | void printBook(struct Book *library)
| ^~~~~~~~~
main.c:5:6: note: previous declaration of ‘printBook’ was here
5 | void printBook(struct Book *library);//打印信息
| ^~~~~~~~~
人造人 发表于 2021-8-8 10:38
你的编译器什么提示也没有?换一个好点的
我的编译器什么都没有,连warning都没
你的报错是说形参传入的格式不一致吗,但都是(struct Book *)类型的啊为什么不行啊。
你scanf 参数不取地址吗
%d-%d-%d
而且这样你输入时也要用-连接 焦糖橙子 发表于 2021-8-8 10:54
我的编译器什么都没有,连warning都没
你的报错是说形参传入的格式不一致吗,但都是(struct Book *)类型 ...
在编译器设置里面找一找
焦糖橙子 发表于 2021-8-8 10:54
我的编译器什么都没有,连warning都没
你的报错是说形参传入的格式不一致吗,但都是(struct Book *)类型 ...
#include<stdio.h>
#include<stdlib.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title;
char author;
float price;
struct Date date;
char publisher;
};
void get_imput(struct Book *library);//录入信息
void printBook(struct Book *library);//打印信息
int main(void)
{
int n;
printf("请输入要录入的书本数量:");
scanf("%d",&n);
struct Book *library;
for(int i=0;i<n;i++)//分配内存空间
{
library=(struct Book*)malloc(sizeof(struct Book));
}
for(int i=0;i<n;i++)
{
printf("正在录入第 %d 本书的信息...\n",i+1);
get_imput(library);
}
printf("====已经全部录入,现在开始打印验证====\n");
for(int i=0;i<n;i++)
{
printf("打印第 %d 本书的信息...\n",i+1);
}
for(int i=0;i<n;i++)//释放内存空间
{
free(library);
}
return 0;
}
void get_imput(struct Book *library)
{
printf("请输入书名:");
scanf(" %s",library->title);
printf("请输入作者:");
scanf(" %s",library->author);
printf("请输入售价:");
scanf("%f",&library->price);
printf("请输入出版日期:");
scanf("%d-%d-%d",&library->date.year,&library->date.month,&library->date.day);
printf("请输入出版社:");
scanf(" %s",library->publisher);
}
void printBook(struct Book *library)
{
printf("书名:《%s》",library->title);
printf("作者:%s",library->author);
printf("售价:%.2f",library->price);
printf("出版日期:%d-%d-%d",library->date.year,library->date.month,library->date.day);
printf("出版社:%s",library->publisher);
}
万千只cnm 发表于 2021-8-8 11:38
你scanf 参数不取地址吗
而且这样你输入时也要用-连接
{:10_266:}谢谢诶,是这个错了 人造人 发表于 2021-8-8 12:17
改好了,我还发现我打印那部分还没写全{:10_245:}
页:
[1]