我的编译器不支持scanf_s,你手动改一下#include<stdio.h>
#include<stdlib.h>
// 结构体以后最好定义在函数外 ,你的也没影响,不过影响 用户阅读
struct Library
{
char name[30];
char author[10];
long int date;
};
int main(void)
{
int i;
struct Library *book = NULL;
int m=1;
while (1)
{
book= (struct Library*)realloc(book, m * sizeof(struct Library));
printf("please input %d name of book(iput -1 exit):", m);
scanf("%s",((book+m-1)->name)); // 输入的是字符串,不需要加&
// 判断语句写到这里比较好,避免用户想退出时还要再输两个值
if(strcmp(((book+m-1)->name), "-1") == 0) // 两个字符串比较,而不是字符与整型常量
{
break;
}
printf("please input %d author of book:", m);
scanf("%s", ((book + m - 1)->author)); // 同上
printf("please input %d date of book:", m);
scanf("%ld", &((book + m - 1)->date));
m++;
/* if(((book+m-1)->name)=="-1")
{
break;
}
m++;
*/
}
for (i = 0; i < m-1; i++)
{
printf("%s\n", (book + i)->name);
printf("%s\n", (book + i)->author);
printf("%ld\n", (book + i)->date);
}
}
|