鱼C论坛

 找回密码
 立即注册
查看: 2305|回复: 2

结构体数据录入以后输出的不对,不知道是哪里出错了

[复制链接]
发表于 2021-4-20 20:52:31 | 显示全部楼层 |阅读模式

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

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

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

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

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

  15. void BookInput(struct Book *book);
  16. void BookPrintf(struct Book *book);

  17. void BookInput(struct Book *book) {
  18.         printf("请输入书名:");
  19.         scanf("%s", book->title);
  20.         printf("请输入作者名:");
  21.         scanf("%s", book->author);
  22.         printf("请输入价格:");
  23.         scanf("%f", &book->price);
  24.         printf("请输入出版日期:");
  25.         scanf("%d %d %d", &book->date.year, &book->date.month, &book->date.day);
  26.         printf("请输入出版社名:");
  27.         scanf("%s", book->publisher);
  28. }

  29. void BookPrint(struct Book *book) {
  30.         printf("书名:%s\n", book->title);
  31.         printf("作者:%s\n", book->author);
  32.         printf("价格:%.2f\n", book->price);
  33.         printf("出版日期:%d年%d月%d日\n", book->date.year, book->date.month, book->date.day);
  34.         printf("出版社:%s\n", book->publisher);
  35. }

  36. int main(void) {
  37.         int count = 0, i, flag;
  38.         struct         Book *library[count];

  39.         printf("欢迎登陆图书录入系统,请录入...\n");
  40.         do {
  41.                 library[count] = (struct Book *)malloc(sizeof(struct Book));
  42.                 BookInput(library[count]);
  43.                 count++;
  44.                 printf("录入成功!选择是否继续录入(1是/0否):");
  45.                 scanf("%d", &flag);
  46.                 getchar();
  47.                 putchar('\n');
  48.         } while (flag);

  49.         printf("\n\n-------你一共录入了%d本书-------\n\n", count);

  50.         for (i = 0; i < count; i++) {
  51.                 BookPrint(library[i]);
  52.                 putchar('\n');
  53.         }

  54.         return 0;
  55. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-20 22:25:12 | 显示全部楼层
你第一次分配内存的时候 count 是 0!!!

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

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

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

  16. void BookInput(struct Book* book);
  17. //void BookPrintf(struct Book* book);
  18. void InputString(char* str)
  19. {
  20.     char c = 0;
  21.     int i = 0;

  22.     while ((c = getchar()) != '\n')
  23.     {
  24.         if (c != '\n')
  25.         {
  26.             str[i] = c;
  27.             i++;
  28.         }
  29.     }
  30.     str[i] = '\0';
  31. }

  32. void BookInput(struct Book* book) {
  33.     printf_s("请输入书名:");
  34.     //scanf_s("%s", &book->title, 2);
  35.     InputString(book->title);
  36.     printf_s("请输入作者名:");
  37.     //scanf_s("%s", &book->author, 2);
  38.     InputString(book->author);
  39.     printf_s("请输入价格:");
  40.     scanf_s("%f", &book->price);
  41.     printf_s("请输入出版日期:");
  42.     scanf_s("%d%*c%d%*c%d%*c", &book->date.year, &book->date.month, &book->date.day);// %*c 过滤中间的字符和最后的\n
  43.     printf_s("请输入出版社名:");
  44.     //scanf_s("%s", &book->publisher, 2);
  45.     InputString(book->publisher);
  46. }

  47. void BookPrint(struct Book* book) {
  48.     printf_s("书名:%s\n", book->title);
  49.     printf_s("作者:%s\n", book->author);
  50.     printf_s("价格:%.2f\n", book->price);
  51.     printf_s("出版日期:%d年%d月%d日\n", book->date.year, book->date.month, book->date.day);
  52.     printf_s("出版社:%s\n", book->publisher);
  53. }

  54. int main(void) {
  55.     int count = 0, i, flag = 1;
  56.     struct Book* library = (struct Book*)malloc(sizeof(struct Book));
  57.     if (!library)
  58.     {
  59.         printf_s("申请空间失败!\n");
  60.         return -1;
  61.     }

  62.     printf_s("欢迎登陆图书录入系统,请录入...\n");
  63.    
  64.     for (i = 1; flag; i++)
  65.     {
  66.         
  67.         if (i != 1)
  68.         {
  69.             struct Book* temp = (struct Book*)realloc(library, i * sizeof(struct Book));    //重新分配内存
  70.             if (!temp)
  71.             {
  72.                 printf_s("申请空间失败!\n");
  73.                 return -1;
  74.             }
  75.             library = temp;
  76.         }

  77.         BookInput(&library[i - 1]);
  78.         count = i;
  79.         printf("录入成功!选择是否继续录入(1是/0否):");
  80.         scanf_s("%d%*c", &flag);
  81.     }

  82.     printf_s("\n\n-------你一共录入了%d本书-------\n\n", count);

  83.     for (i = 0; i < count; i++) {
  84.         BookPrint(&library[i]);
  85.         //putchar('\n');
  86.     }

  87.     free(library);
  88.     return 0;
  89. }
复制代码


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

使用道具 举报

发表于 2021-4-20 22:31:01 | 显示全部楼层
字符串输入我重写了个方法  scanf我试的时候总是不大好使
还有scanf后面我都加了个%*c  过滤 \n
不然这个 \n 会带到下一次输入
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-29 22:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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