c语言图书借阅系统录入出问题

在添加书籍时输入三个数据后回车程序直接关闭

img

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Book{
    int id;
    char title[50];
    char author[50];
    int available;
} ;
Book books[MAX_BOOKS];
void menu()
{
    system("cls");//清屏
    system("color f0");//改变背景颜色
    printf(" ————————————————————————————\n");
    printf("                        请选择序号                         \n");
    printf(" ————————————————————————————\n");
    printf("                      1. 添加图书                         \n");
    printf(" ————————————————————————————\n");
    printf("                      2. 删除图书                         \n");
    printf(" ————————————————————————————\n");
    printf("                      3. 显示图书信息                       \n");
    printf(" ————————————————————————————\n");
    printf("                      4. 添加学生                      \n");
    printf(" ————————————————————————————\n");
    printf("                      5. 删除学生                     \n");
    printf(" ————————————————————————————\n");
    printf("                      6. 显示学生名单                     \n");
    printf(" ————————————————————————————\n");
    printf("                      7. 借阅图书                    \n");
    printf(" ————————————————————————————\n");
    printf("                      8. 归还图书                   \n");
    printf(" ————————————————————————————\n");
    printf("                      9. 显示学生借阅图书                 \n");
    printf(" ————————————————————————————\n");
    printf("                      0. 退出                             \n");
    printf(" ————————————————————————————\n");
}
void add_book() {//添加图书
    system("cls");
    char title [50];
    printf("输入书名,作者,图书编号:\n ");
    printf("以输入“over”为结束录入\n");
    while(scanf("%s",title),strcmp(strlwr(title),"over")!=0){
        strcpy( books[book_count].title,title);
        scanf("%s",books[book_count].id);
        scanf("%s",books[book_count].author);
        books[book_count].available = 1;
        book_count++;
    }
}
int main() {
    int choice;
    while (1) {
        menu();
        printf("请选择: ");
        scanf("%d", &choice);

        switch (choice) {
        case 1:
            add_book();
            break;
        }      
}

```


根据您提供的代码,可能存在以下问题:

缺少头文件和定义:在代码中使用了MAX_BOOKS、book_count等变量,但是没有给出相应的头文件和定义。需要添加相应的头文件和定义,例如:
c
Copy
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
int book_count = 0;
struct Book{
    int id;
    char title[50];
    char author[50];
    int available;
} ;
Book books[MAX_BOOKS];
函数未定义:在代码中调用了add_book()函数,但是该函数未在代码中进行定义。需要添加add_book()函数的定义。
c
Copy
void add_book() {//添加图书
    system("cls");
    char title [50];
    printf("输入书名,作者,图书编号:\n ");
    printf("以输入“over”为结束录入\n");
    while(scanf("%s",title),strcmp(strlwr(title),"over")!=0){
        strcpy( books[book_count].title,title);
        scanf("%d",&books[book_count].id);
        scanf("%s",books[book_count].author);
        books[book_count].available = 1;
        book_count++;
    }
}
scanf()参数类型错误:在add_book()函数中,读取图书编号时使用了%s格式,导致读取的是字符串,而不是数字。需要修改为%d格式,以正确读取数字。
c
Copy
scanf("%d",&books[book_count].id);
无限循环:在main()函数中,使用了一个无限循环,但是没有给出退出循环的条件。需要在循环中添加退出条件,例如:
c
Copy
while (1) {
    menu();
    printf("请选择: ");
    scanf("%d", &choice);

    switch (choice) {
    case 1:
        add_book();
        break;
    case 0:
        exit(0);
    default:
        printf("输入有误,请重新输入!\n");
        break;
    }      
}
以上是可能的解决方案,具体解决方法可能需要根据代码的具体情况进行调整和优化。

程序崩溃了
scanf("%s",books[book_count].id);
id是int型,要用%d输入