c语言结构体出现了问题

include<stdio.h>

struct Date
{
int year;
int month;
int day;
} date;

struct Book
{

char title[128];
char author[40];
float price;
struct Date date;
char publisher[40];

} b1,b2,book;

/这是输入函数/struct Book getinput(struct Book book);
/这是打印函数/void printBook(struct Book book);

struct Book getinput(struct Book book)
{
printf("请输入书名:");
scanf("%s",book.title);
printf("请输入作者:");
scanf("%s",book.author);
printf("请输入售价:");
scanf("%f",book.price);
printf("请输入出版日期:");
scanf("%d-%d-%d",book.date.year,book.date.month,book.date.day);
printf("请输入出版社:");
scanf("%s",book.publisher);

return book;

}

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(void)
{
printf("请输入第一本书:");
b1 = getinput(b1);
putchar("\n");
printf("请输入第二本书:");
b2 = getinput(b2);

printf("开始打印。。。。。");
printf("打印第一本书的信息");
printBook(b1);
putchar("/n");
printf("打印第二本书的信息");
printBook(b2);
return 0;

}

为什么在这里他就不动了?我不知道为啥不能插入代码,所以有些难看

回答

#include<stdio.h>

struct Date
{
    int year;
    int month;
    int day;
} date;

struct Book
{
    char title[128];
    char author[40];
    float price;
    struct Date date;
    char publisher[40];
} b1, b2, book;

/* 这是输入函数 */ struct Book getinput(struct Book book);
/* 这是打印函数 */ void printBook(struct Book book);

struct Book getinput(struct Book book)
{
    printf("请输入书名:");
    scanf("%s", book.title);
    printf("请输入作者:");
    scanf("%s", book.author);
    printf("请输入售价:");
    scanf("%f", &book.price);
    printf("请输入出版日期:");
    scanf("%d-%d-%d", &book.date.year, &book.date.month, &book.date.day);
    printf("请输入出版社:");
    scanf("%s", book.publisher);

    return book;
}

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(void)
{
    printf("请输入第一本书:");
    b1 = getinput(b1);
    putchar('\n');
    printf("请输入第二本书:");
    b2 = getinput(b2);

    printf("\n开始打印。。。。。\n");

    printf("打印第一本书的信息\n");
    printBook(b1);
    putchar('\n');
    printf("打印第二本书的信息\n");
    printBook(b2);

    return 0;
}
以上是c语言结构体出现了问题的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>