鱼C论坛

 找回密码
 立即注册
查看: 1178|回复: 8

[已解决]书本信息录入(结构体问题)

[复制链接]
发表于 2021-8-8 09:55:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 焦糖橙子 于 2021-8-8 09:57 编辑

建立一个图书馆,录入书本信息

我在录入售价的时候出了问题,能看看哪里写错了吗?



  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. void get_imput(struct Book *library);//录入信息
  4. void printBook(struct Book *library);//打印信息

  5. struct Date
  6. {
  7.         int year;
  8.         int month;
  9.         int day;
  10. };

  11. struct Book
  12. {
  13.         char title[128];
  14.         char author[40];
  15.         float price;
  16.         struct Date date;
  17.         char publisher[40];
  18. };



  19. int main(void)
  20. {
  21.         int n;
  22.         struct Book *library[n];
  23.        
  24.         printf("请输入要录入的书本数量:");
  25.         scanf("%d",&n);
  26.        
  27.         for(int i=0;i<n;i++)//分配内存空间
  28.         {
  29.                 library[i]=(struct Book*)malloc(sizeof(struct Book));
  30.         }
  31.        
  32.         for(int i=0;i<n;i++)
  33.         {
  34.                 printf("正在录入第 %d 本书的信息...\n",i+1);
  35.                 get_imput(library[i]);
  36.         }
  37.        
  38.         printf("====已经全部录入,现在开始打印验证====\n");
  39.         for(int i=0;i<n;i++)
  40.         {
  41.                 printf("打印第 %d 本书的信息...\n",i+1);
  42.                
  43.         }
  44.        
  45.         for(int i=0;i<n;i++)//释放内存空间
  46.         {
  47.                 free(library[i]);
  48.         }
  49.        
  50.         return 0;
  51. }

  52. void get_imput(struct Book *library)
  53. {
  54.         printf("请输入书名:");
  55.         scanf(" %s",library->title);
  56.         printf("请输入作者:");
  57.         scanf(" %s",library->author);
  58.         printf("请输入售价:");
  59.         scanf("%f",library->price);
  60.         printf("请输入出版日期:");
  61.         scanf("%d-%d-%d",library->date.year,library->date.month,library->date.day);
  62.         printf("请输入出版社:");
  63.         scanf(" %s",library->publisher);
  64. }

  65. void printBook(struct Book *library)
  66. {
  67.         printf("书名:《%s》",library->title);
  68.         printf("作者:%s",library->author);
  69.         printf("售价:%.2f",library->price);
  70.         printf("出版日期:%d-%d-%d",library->date.year,library->date.month,library->date.day);
  71.         printf("出版社:%s",library->publisher);
  72. }
复制代码
最佳答案
2021-8-8 12:17:44
焦糖橙子 发表于 2021-8-8 10:54
我的编译器什么都没有,连warning都没
你的报错是说形参传入的格式不一致吗,但都是(struct Book *)类型 ...
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. struct Date
  4. {
  5.     int year;
  6.     int month;
  7.     int day;
  8. };

  9. struct Book
  10. {
  11.     char title[128];
  12.     char author[40];
  13.     float price;
  14.     struct Date date;
  15.     char publisher[40];
  16. };

  17. void get_imput(struct Book *library);//录入信息
  18. void printBook(struct Book *library);//打印信息

  19. int main(void)
  20. {
  21.     int n;

  22.     printf("请输入要录入的书本数量:");
  23.     scanf("%d",&n);
  24.     struct Book *library[n];

  25.     for(int i=0;i<n;i++)//分配内存空间
  26.     {
  27.         library[i]=(struct Book*)malloc(sizeof(struct Book));
  28.     }

  29.     for(int i=0;i<n;i++)
  30.     {
  31.         printf("正在录入第 %d 本书的信息...\n",i+1);
  32.         get_imput(library[i]);
  33.     }

  34.     printf("====已经全部录入,现在开始打印验证====\n");
  35.     for(int i=0;i<n;i++)
  36.     {
  37.         printf("打印第 %d 本书的信息...\n",i+1);

  38.     }

  39.     for(int i=0;i<n;i++)//释放内存空间
  40.     {
  41.         free(library[i]);
  42.     }

  43.     return 0;
  44. }

  45. void get_imput(struct Book *library)
  46. {
  47.     printf("请输入书名:");
  48.     scanf(" %s",library->title);
  49.     printf("请输入作者:");
  50.     scanf(" %s",library->author);
  51.     printf("请输入售价:");
  52.     scanf("%f",&library->price);
  53.     printf("请输入出版日期:");
  54.     scanf("%d-%d-%d",&library->date.year,&library->date.month,&library->date.day);
  55.     printf("请输入出版社:");
  56.     scanf(" %s",library->publisher);
  57. }

  58. void printBook(struct Book *library)
  59. {
  60.     printf("书名:《%s》",library->title);
  61.     printf("作者:%s",library->author);
  62.     printf("售价:%.2f",library->price);
  63.     printf("出版日期:%d-%d-%d",library->date.year,library->date.month,library->date.day);
  64.     printf("出版社:%s",library->publisher);
  65. }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-8 10:36:22 | 显示全部楼层
scanf("%d-%d-%d",library->date.year,library->date.month,library->date.day);
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-8 10:38:14 | 显示全部楼层
你的编译器什么提示也没有?换一个好点的
  1. $ gcc -g -Wall -o main main.c
  2. main.c:4:23: warning: ‘struct Book’ declared inside parameter list will not be visible outside of this definition or declaration
  3.     4 | void get_imput(struct Book *library);//录入信息
  4.       |                       ^~~~
  5. main.c:5:23: warning: ‘struct Book’ declared inside parameter list will not be visible outside of this definition or declaration
  6.     5 | void printBook(struct Book *library);//打印信息
  7.       |                       ^~~~
  8. main.c: In function ‘main’:
  9. main.c:41:26: warning: passing argument 1 of ‘get_imput’ from incompatible pointer type [-Wincompatible-pointer-types]
  10.    41 |         get_imput(library[i]);
  11.       |                   ~~~~~~~^~~
  12.       |                          |
  13.       |                          struct Book *
  14. main.c:4:29: note: expected ‘struct Book *’ but argument is of type ‘struct Book *’
  15.     4 | void get_imput(struct Book *library);//录入信息
  16.       |                ~~~~~~~~~~~~~^~~~~~~
  17. main.c: At top level:
  18. main.c:59:6: error: conflicting types for ‘get_imput’
  19.    59 | void get_imput(struct Book *library)
  20.       |      ^~~~~~~~~
  21. main.c:4:6: note: previous declaration of ‘get_imput’ was here
  22.     4 | void get_imput(struct Book *library);//录入信息
  23.       |      ^~~~~~~~~
  24. main.c: In function ‘get_imput’:
  25. main.c:66:13: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
  26.    66 |     scanf("%f",library->price);
  27.       |            ~^  ~~~~~~~~~~~~~~
  28.       |             |         |
  29.       |             float *   double
  30. main.c:68:13: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
  31.    68 |     scanf("%d-%d-%d",library->date.year,library->date.month,library->date.day);
  32.       |            ~^        ~~~~~~~~~~~~~~~~~~
  33.       |             |                     |
  34.       |             int *                 int
  35. main.c:68:16: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat=]
  36.    68 |     scanf("%d-%d-%d",library->date.year,library->date.month,library->date.day);
  37.       |               ~^                        ~~~~~~~~~~~~~~~~~~~
  38.       |                |                                     |
  39.       |                int *                                 int
  40. main.c:68:19: warning: format ‘%d’ expects argument of type ‘int *’, but argument 4 has type ‘int’ [-Wformat=]
  41.    68 |     scanf("%d-%d-%d",library->date.year,library->date.month,library->date.day);
  42.       |                  ~^                                         ~~~~~~~~~~~~~~~~~
  43.       |                   |                                                      |
  44.       |                   int *                                                  int
  45. main.c: At top level:
  46. main.c:73:6: error: conflicting types for ‘printBook’
  47.    73 | void printBook(struct Book *library)
  48.       |      ^~~~~~~~~
  49. main.c:5:6: note: previous declaration of ‘printBook’ was here
  50.     5 | void printBook(struct Book *library);//打印信息
  51.       |      ^~~~~~~~~
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-8 10:54:39 | 显示全部楼层
人造人 发表于 2021-8-8 10:38
你的编译器什么提示也没有?换一个好点的

我的编译器什么都没有,连warning都没
你的报错是说形参传入的格式不一致吗,但都是(struct Book *)类型的啊为什么不行啊。

Screenshot 2021-08-08 105303.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-8 11:38:34 | 显示全部楼层
你scanf 参数不取地址吗
  1. %d-%d-%d
复制代码

而且这样你输入时也要用-连接
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-8 12:14:13 | 显示全部楼层
焦糖橙子 发表于 2021-8-8 10:54
我的编译器什么都没有,连warning都没
你的报错是说形参传入的格式不一致吗,但都是(struct Book *)类型 ...

在编译器设置里面找一找
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-8 12:17:44 | 显示全部楼层    本楼为最佳答案   
焦糖橙子 发表于 2021-8-8 10:54
我的编译器什么都没有,连warning都没
你的报错是说形参传入的格式不一致吗,但都是(struct Book *)类型 ...
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. struct Date
  4. {
  5.     int year;
  6.     int month;
  7.     int day;
  8. };

  9. struct Book
  10. {
  11.     char title[128];
  12.     char author[40];
  13.     float price;
  14.     struct Date date;
  15.     char publisher[40];
  16. };

  17. void get_imput(struct Book *library);//录入信息
  18. void printBook(struct Book *library);//打印信息

  19. int main(void)
  20. {
  21.     int n;

  22.     printf("请输入要录入的书本数量:");
  23.     scanf("%d",&n);
  24.     struct Book *library[n];

  25.     for(int i=0;i<n;i++)//分配内存空间
  26.     {
  27.         library[i]=(struct Book*)malloc(sizeof(struct Book));
  28.     }

  29.     for(int i=0;i<n;i++)
  30.     {
  31.         printf("正在录入第 %d 本书的信息...\n",i+1);
  32.         get_imput(library[i]);
  33.     }

  34.     printf("====已经全部录入,现在开始打印验证====\n");
  35.     for(int i=0;i<n;i++)
  36.     {
  37.         printf("打印第 %d 本书的信息...\n",i+1);

  38.     }

  39.     for(int i=0;i<n;i++)//释放内存空间
  40.     {
  41.         free(library[i]);
  42.     }

  43.     return 0;
  44. }

  45. void get_imput(struct Book *library)
  46. {
  47.     printf("请输入书名:");
  48.     scanf(" %s",library->title);
  49.     printf("请输入作者:");
  50.     scanf(" %s",library->author);
  51.     printf("请输入售价:");
  52.     scanf("%f",&library->price);
  53.     printf("请输入出版日期:");
  54.     scanf("%d-%d-%d",&library->date.year,&library->date.month,&library->date.day);
  55.     printf("请输入出版社:");
  56.     scanf(" %s",library->publisher);
  57. }

  58. void printBook(struct Book *library)
  59. {
  60.     printf("书名:《%s》",library->title);
  61.     printf("作者:%s",library->author);
  62.     printf("售价:%.2f",library->price);
  63.     printf("出版日期:%d-%d-%d",library->date.year,library->date.month,library->date.day);
  64.     printf("出版社:%s",library->publisher);
  65. }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2021-8-8 16:15:39 | 显示全部楼层
万千只cnm 发表于 2021-8-8 11:38
你scanf 参数不取地址吗

而且这样你输入时也要用-连接

谢谢诶,是这个错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-8 16:25:57 | 显示全部楼层

改好了,我还发现我打印那部分还没写全
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 21:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表