程序不会报错,但在输入第一个数后就没有后续。
#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 getinput(struct Book *book);
void pb(struct Book *book);
void initlbr(struct Book *lbr[],int *b);
void plbr(struct Book *lbr[],int *b);
void release(struct Book *lbr[],int *b);
void getinput(struct Book *book)
{
printf("name:");
scanf("%s",book->title);
printf("author:");
scanf("%s",book->author);
printf("price:");
scanf("%f",&book->price);
printf("day:");
scanf("%d-%d-%d",&book->date.year,&book->date.month,&book->date.day);
printf("pr:");
scanf("%s",book->publisher);
}
void pb(struct Book *book)
{
printf("name:%s\n",book->title);
printf("author:%s\n",book->author);
printf("price:%.2f\n",book->price);
printf("day:%d-%d-%d\n",book->date.year,book->date.month,book->date.day);
printf("pr:%s\n",book->publisher);
}
void initlbr(struct Book *lbr[],int *b)
{
int i;
for(i = 0;i < *b;i++)
{
lbr[i] = NULL;
}
}
void plbr(struct Book *lbr[],int *b)
{
int i;
for(i = 0;i < *b;i++)
{
if(lbr[i] != NULL)
{
pb(lbr[i]);
putchar('\n');
}
}
}
void release(struct Book *lbr[],int *b)
{
int i;
for(i = 0;i < *b;i++)
{
if(lbr[i] != NULL);
{
free(lbr[i]);
}
}
}
int main(void)
{
int *b;
int c;
printf("Please set the maxium of books in the lbr:");
scanf("d",&c);
b = &c;
struct Book *lbr[c];
struct Book *ptr = NULL;
int a,index = 0;
initlbr(lbr,b);
while(1)
{
printf("Do you want to register a book?\n");
do
{
a = getchar();
}while(a != 'y' && a != 'n');
if (a == 'y')
{
if (index < *b)
{
ptr = (struct Book *)malloc(sizeof(struct Book));
getinput(ptr);
lbr[index] = ptr;
index++;
putchar('\n');
}
}
else
{
printf("no more space");
break;
}
}
printf("over");
plbr(lbr,b);
release(lbr,b);
return 0;
}