|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位大佬,我这个程序什么地方写错了啊?
为什么不能用,打印出来都是乱码,不然就是报错...
#include<stdio.h>
#include<stdlib.h>
#include<string.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 getInput(struct Book* book) {
printf("请输入书名: ");
scanf_s("%s", book->title, 128);
printf("请输入作者: ");
scanf_s("%s", book->author, 40);
printf("请输入售价: ");
scanf_s("%f", &book->price);
printf("请输入出版日期: ");
scanf_s("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
printf("请输入出版社: ");
scanf_s("%s", book->publisher, 40);
};
void printfBook(struct Book* book);
void printfBook(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)
{
struct Book *tsg[10];
struct Book *tsg1;
int num = 0;
int count = 0;
do
{
count++;
tsg1 = (struct Book *)malloc(sizeof(struct Book));
if (tsg == NULL)
{
printf("存储出错!\n");
exit(1);
}
printf("请录入第%d本书的信息...\n",count);
getInput(tsg1);
putchar('\n');
printf("如果结束请输入1");
scanf_s("%d", &num);
tsg[count] = tsg1;
free(tsg1);
} while (num != 10);
int i;
printf("\n\n录入完毕,现在开始打印验证...\n\n");
for (i = 0; i < count; i++)
{
printf("打印第%d本书的信息...\n",i+1);
printfBook(tsg[i]);
putchar('\n');
}
return 0;
}
1,tsg[count] = tsg1;错了,应该为tsg[count-1] = tsg1;2,free(tsg1);这句话不要。如果释放了,那你输入的数据就没了。
|
|