鱼C论坛

 找回密码
 立即注册
查看: 1614|回复: 10

[已解决]为什么这个编译不通过呢?找不出错误?希望帮帮忙

[复制链接]
发表于 2018-12-14 14:47:30 | 显示全部楼层 |阅读模式

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

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

x
为什么这个编译不通过呢?找不出错误?希望帮帮忙
最佳答案
2018-12-15 12:17:57
我帮你改了一下

  1. #include<stdio.h>

  2. void ReadBook(struct Book *);
  3. void PrintBook(struct Book *);
  4. struct Date
  5. {
  6.         int year;
  7.         int month;
  8.         int day;
  9. };
  10. struct Book
  11. {
  12.         char title[128];
  13.         char author[40];
  14.         float price;
  15.         struct Date date;
  16.         char publisher[128];
  17. };
  18. void ReadBook(struct Book *book)
  19. {
  20.         printf("请输入书名:");
  21.         scanf("%s", book->title);
  22.         printf("请输入作者:");
  23.         scanf("%s", book->author);
  24.         printf("请输入售价:");
  25.         scanf("%f", &book->price);
  26.         printf("请输入出版日期:");
  27.         scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  28.         printf("请输入出版社:");
  29.         scanf("%s", book->publisher);
  30. }
  31. void PrintBook(struct Book book)
  32. {
  33.         printf("书名:%s\n", book.title);
  34.         printf("作者:%s\n", book.author);
  35.         printf("售价:%.2f\n", book.price);
  36.         printf("出版日期:%d-%d-%d\n", book.date.year, book.date.month, book.date.day);
  37.         printf("出版社:%s\n", book.publisher);
  38. }
  39. int main()
  40. {
  41.         int N, i;
  42.         printf("欢迎光临图书馆!\n");
  43.         printf("请输入需录入图书数:\n");

  44.         scanf("%d", &N);
  45.         putchar('\n');
  46.         struct Book *pbook = (struct Book*)malloc(N * sizeof(struct Book));  //在堆上动态分配内存
  47.         for (i = 0; i<N; i++)
  48.         {
  49.                 printf("请录入第%d本图书信息....\n", i );
  50.                 ReadBook(&pbook[i]);
  51.         }
  52.         putchar('\n');
  53.         printf("图书信息录入完毕,现在开始打印验证....\n");
  54.         for (i = 0; i<N; i++)
  55.         {
  56.                 printf("打印第%d本图书信息\n", i );
  57.                         PrintBook(pbook[i]);
  58.         }
  59.         putchar('\n');
  60.         printf("===打印完毕===\n");
  61.         return 0;
  62. }
复制代码
QQ截图20181214144428.png
QQ截图20181214144517.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-12-14 16:16:07 | 显示全部楼层
贴代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-14 19:41:59 | 显示全部楼层
本帖最后由 TyCk 于 2018-12-14 19:44 编辑

第三行和第十九行对ReadBook的声明存在冲突,参数书写有问题。
试着修改一下*的位置,感觉有点问题。
另外,尝试修改下Book结构体声明的语句的位置,放在第三行前面呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-14 20:09:28 | 显示全部楼层
#include<stdio.h>

void ReadBook(struct Book *);
void PrintBook(struct Book *);
struct Date
{
        int year;
        int month;
        int day
};
struct Book
{
        char title[128];
        char author[40];
        float price;
        struct Date date;
        char publisher[128]
};
void ReadBook(struct Book *book)
{
        printf("请输入书名:");
        scanf("%s",book->title);
        printf("请输入作者:");
        scanf("%s",book->author);
        printf("请输入售价:");
        scanf("%.2f",&book->price);
        printf("请输入出版日期:");
        scanf("%d-%d-%d",&book->Date.year,&book->Date.month,&book->Date.day);
        printf("请输入出版社:");
        scanf("%s",book->publisher);
}
void PrintBook(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);
}
int main()
{
        int N,i;
        printf("欢迎光临图书馆!\n");
        printf("请输入需录入图书数:\n");
        scanf("%d",&N);
        putchar('\n');
        struct Book book[N];
        struct Book*pbook[N]
        for(i=0;i<N;i++)
        {
                pbook[i]=&book[i];
        }
        for(i=0;i<N;i++)
        {
                printf("请录入第%d本图书信息....\n",i+);
                ReadBook(pbook[i]);
        }
        putchar('\n');
        printf("图书信息录入完毕,现在开始打印验证....\n");
        for(i=0;i<N;i++)
        {
                printf("打印第%d本图书信息\n",i+);
                PrintfBook(pbook[i]);
        }
        putchar('\n');
        printf("===打印完毕===\n");
       
       
       
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-14 21:23:12 | 显示全部楼层
第9行,第17行:缺少 ; 结尾。
第28行,第37行:Date.year 错误,应该是date.year 。。。。
第47行,第48行(struct Book book[N];struct Book*pbook[N]):c语言不支持动态分配数组。后面还缺少 ;
55行:i+  ??  i ......
第62行:i+  ??  i ......   中文 ;应该改成英文 ;
63行: PrintfBook(pbook[i]); ==>  PrintBook(pbook[i]);
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-14 21:50:26 | 显示全部楼层
我当时看视频和题主有同样的问题,大概率视频那个代码写的有问题吧,毕竟他用的Linux
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-14 21:56:51 | 显示全部楼层
百里狂生 发表于 2018-12-14 21:23
第9行,第17行:缺少 ; 结尾。
第28行,第37行:Date.year 错误,应该是date.year 。。。。
第47行,第48 ...

照着改了,但还是报错,动态分配数组好像是可行的
#include<stdio.h>

void ReadBook(struct Book *);
void PrintBook(struct Book *);
struct Date
{
        int year;
        int month;
        int day;
};
struct Book
{
        char title[128];
        char author[40];
        float price;
        struct Date date;
        char publisher[128];
};
void ReadBook(struct Book *book)
{
        printf("请输入书名:");
        scanf("%s",book->title);
        printf("请输入作者:");
        scanf("%s",book->author);
        printf("请输入售价:");
        scanf("%.2f",&book->price);
        printf("请输入出版日期:");
        scanf("%d-%d-%d",&book->date.year,&book->date.month,&book->date.day);
        printf("请输入出版社:");
        scanf("%s",book->publisher);
}
void PrintBook(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);
}
int main()
{
        int N,i;
        printf("欢迎光临图书馆!\n");
        printf("请输入需录入图书数:\n");
        scanf("%d",&N);
        putchar('\n');
        struct Book book[N];
        struct Book*pbook[N];
        for(i=0;i<N;i++)
        {
                pbook=&book;
        }
        for(i=0;i<N;i++)
        {
                printf("请录入第%d本图书信息....\n",i+1);
                ReadBook(pbook);
        }
        putchar('\n');
        printf("图书信息录入完毕,现在开始打印验证....\n");
        for(i=0;i<N;i++)
        {
                printf("打印第%d本图书信息\n",i+);
                PrintBook(pbook);
        }
        putchar('\n');
        printf("===打印完毕===\n");
       
       
       
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-15 12:17:57 | 显示全部楼层    本楼为最佳答案   
我帮你改了一下

  1. #include<stdio.h>

  2. void ReadBook(struct Book *);
  3. void PrintBook(struct Book *);
  4. struct Date
  5. {
  6.         int year;
  7.         int month;
  8.         int day;
  9. };
  10. struct Book
  11. {
  12.         char title[128];
  13.         char author[40];
  14.         float price;
  15.         struct Date date;
  16.         char publisher[128];
  17. };
  18. void ReadBook(struct Book *book)
  19. {
  20.         printf("请输入书名:");
  21.         scanf("%s", book->title);
  22.         printf("请输入作者:");
  23.         scanf("%s", book->author);
  24.         printf("请输入售价:");
  25.         scanf("%f", &book->price);
  26.         printf("请输入出版日期:");
  27.         scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  28.         printf("请输入出版社:");
  29.         scanf("%s", book->publisher);
  30. }
  31. void PrintBook(struct Book book)
  32. {
  33.         printf("书名:%s\n", book.title);
  34.         printf("作者:%s\n", book.author);
  35.         printf("售价:%.2f\n", book.price);
  36.         printf("出版日期:%d-%d-%d\n", book.date.year, book.date.month, book.date.day);
  37.         printf("出版社:%s\n", book.publisher);
  38. }
  39. int main()
  40. {
  41.         int N, i;
  42.         printf("欢迎光临图书馆!\n");
  43.         printf("请输入需录入图书数:\n");

  44.         scanf("%d", &N);
  45.         putchar('\n');
  46.         struct Book *pbook = (struct Book*)malloc(N * sizeof(struct Book));  //在堆上动态分配内存
  47.         for (i = 0; i<N; i++)
  48.         {
  49.                 printf("请录入第%d本图书信息....\n", i );
  50.                 ReadBook(&pbook[i]);
  51.         }
  52.         putchar('\n');
  53.         printf("图书信息录入完毕,现在开始打印验证....\n");
  54.         for (i = 0; i<N; i++)
  55.         {
  56.                 printf("打印第%d本图书信息\n", i );
  57.                         PrintBook(pbook[i]);
  58.         }
  59.         putchar('\n');
  60.         printf("===打印完毕===\n");
  61.         return 0;
  62. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-15 14:23:57 | 显示全部楼层
第三行和第十九行对ReadBook的声明存在冲突,参数书写有问题。
试着修改一下*的位置,感觉有点问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-15 17:20:31 | 显示全部楼层

也报错,而且第52行不用&吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-15 17:38:54 | 显示全部楼层
我知道了,结构体声明在函数之前才对
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-17 00:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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