m0_64375392 2022-01-10 16:05 采纳率: 63.6%
浏览 20
已结题

为什么只能运行两次就自动退出了

#include "stdio.h"
#include<stdlib.h>
#include<string.h>
struct book
{
char bookname[20];
char author[20];
struct book *next;
};
void input(struct book **library)
{
struct book *p;
static struct book q;
p=(struct book
)malloc(sizeof(struct book));
if(p==NULL)
{
printf("内存分配错误");
}
printf("请输入书名:");
scanf("%s",p->bookname);
printf("请输入作者:");
scanf("%s",p->author);
if(*library==NULL)
{
*library=p;
p=p->next;
}
else
{
q->next=p;
p->next=NULL;
}
q=p;
}
int main()
{
struct book *library=NULL;
char i;
while(1)
{
printf("是否输入书籍?");
fflush(stdin);
i=getchar();
fflush(stdin);
if(i=='Y'||i=='y')
{
input(&library);
}
}
return 0;
}

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2022-01-10 16:20
    关注
    void input(struct book **library)
    {
    struct book *p;
    static struct book *q;
    p=(struct book)malloc(sizeof(struct book));
    if(p==NULL)
    {
    printf("内存分配错误");
    }
    p->next = NULL;
    printf("请输入书名:");
    scanf("%s",p->bookname);
    printf("请输入作者:");
    scanf("%s",p->author);
    if(*library==NULL)
        *library=p;
    else
        q->next=p;
    q = p;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 1月18日
  • 已采纳回答 1月10日
  • 创建了问题 1月10日

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建