鱼C论坛

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

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

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

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

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

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

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

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


#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[128];
        char author[40];
        float price;
        struct Date date;
        char publisher[40];
};



int main(void)
{
        int n;
        struct Book *library[n];
        
        printf("请输入要录入的书本数量:");
        scanf("%d",&n);
        
        for(int i=0;i<n;i++)//分配内存空间
        {
                library[i]=(struct Book*)malloc(sizeof(struct Book));
        }
        
        for(int i=0;i<n;i++)
        {
                printf("正在录入第 %d 本书的信息...\n",i+1);
                get_imput(library[i]);
        }
        
        printf("====已经全部录入,现在开始打印验证====\n");
        for(int i=0;i<n;i++)
        {
                printf("打印第 %d 本书的信息...\n",i+1);
                
        }
        
        for(int i=0;i<n;i++)//释放内存空间
        {
                free(library[i]);
        }
        
        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);
 } 
 
最佳答案
2021-8-8 12:17:44
焦糖橙子 发表于 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[128];
    char author[40];
    float price;
    struct Date date;
    char publisher[40];
};

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

int main(void)
{
    int n;

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

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

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

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

    }

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

    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);
} 
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-8 10:36:22 | 显示全部楼层
scanf("%d-%d-%d",library->date.year,library->date.month,library->date.day);
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-8 10:38:14 | 显示全部楼层
你的编译器什么提示也没有?换一个好点的
$ 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[i]);
      |                   ~~~~~~~^~~
      |                          |
      |                          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);//打印信息
      |      ^~~~~~~~~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

Screenshot 2021-08-08 105303.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-8 11:38:34 | 显示全部楼层
你scanf 参数不取地址吗
%d-%d-%d
而且这样你输入时也要用-连接
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

在编译器设置里面找一找
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-8 12:17:44 | 显示全部楼层    本楼为最佳答案   
焦糖橙子 发表于 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[128];
    char author[40];
    float price;
    struct Date date;
    char publisher[40];
};

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

int main(void)
{
    int n;

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

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

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

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

    }

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

    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);
} 
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

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

谢谢诶,是这个错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

改好了,我还发现我打印那部分还没写全
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 22:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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