鱼C论坛

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

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

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

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

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

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

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

struct Book {
        char title[40];
        char author[40];
        float price;
        struct Date date;
        char publisher[40];
};

void BookInput(struct Book *book);
void BookPrintf(struct Book *book);

void BookInput(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);
}

void BookPrint(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) {
        int count = 0, i, flag;
        struct         Book *library[count];

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

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

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

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-4-20 22:25:12 | 显示全部楼层
你第一次分配内存的时候 count 是 0!!!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

struct Book {
    char title[40];
    char author[40];
    float price;
    struct Date date;
    char publisher[40];
};

void BookInput(struct Book* book);
//void BookPrintf(struct Book* book);
void InputString(char* str)
{
    char c = 0;
    int i = 0;

    while ((c = getchar()) != '\n')
    {
        if (c != '\n')
        {
            str[i] = c;
            i++;
        }
    }
    str[i] = '\0';
}

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

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

int main(void) {
    int count = 0, i, flag = 1;
    struct Book* library = (struct Book*)malloc(sizeof(struct Book));
    if (!library)
    {
        printf_s("申请空间失败!\n");
        return -1;
    }

    printf_s("欢迎登陆图书录入系统,请录入...\n");
    
    for (i = 1; flag; i++)
    {
        
        if (i != 1)
        {
            struct Book* temp = (struct Book*)realloc(library, i * sizeof(struct Book));    //重新分配内存
            if (!temp)
            {
                printf_s("申请空间失败!\n");
                return -1;
            }
            library = temp;
        }

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

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

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

    free(library);
    return 0;
}

1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 15:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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