小弟在做C程序设计~图书管理系统
int sum;
struct book /*定义书的类型*/
{ int number; /*序号*/
char name[10]; /*书名*/
char author[10]; /*作者*/
char from[10]; /*出版社*/
float price; /*价格*/
}book[1000];
...
(添加图书时)
printf("请你输入书的价格\n");
scanf("%f",&book[i].price);
在到输入价格时就出现:
" scanf : floating point formats not linked
Abnormal program termination
Press any key to continue . . ."
很是郁闷
单把%f改成%d就不会出现这种情况
但无论输入价格是多少~输出的总是0(%d)或者0.00000(%f)
怎么解决啊....
下面是我的程序
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
int sum;
struct book /*定义书的类型*/
{ int number; /*序号*/
char name[10]; /*书名*/
char author[10]; /*作者*/
char from[10]; /*出版社*/
float price; /*价格*/
}book[1000];
main() /*主函数*/
{ char choice,ch1,ch2,ch3,ch4;
bioskey(0);
do
{clrscr();
printf("\n\n\n\t ————————欢迎进入图书管理系统————————\n\n");
printf("(^o^)(^o^)(^o^)(^o^)(^o^)(^o^)(^o^)(^o^)(^o^)(^o^)(^o^)(^o^)(^o^)(^o^)(^o^)(^o^)\n");
printf("0: 查阅图书信息\n");
printf("1:修改图书信息\n");
printf("2:增加图书\n");
printf("3:删除图书\n");
printf("4. 图书信息统计\n");
printf("5. 退出系统\n");
printf("请输入0-5\n");
scanf("%s",&choice);
switch(choice)
{
case '0': viewbook();break;
case '1': changebook();break;
case '2': addbook();break;
case '3': delbook();break;
case '4': bookcount();getch();break;
case '5': break;
default:printf("您输入了错误的操作,请重新输入\n");getch();break;
}
}while(choice!='5');
}
viewbook() /*查阅功能*/
{
char ch1;
do
{
printf("——————————欢迎进入图书查询系统!——————————\n");
printf(" 1:按书名查找\n");
printf(" 2: 按作者查找\n");
printf(" 0:返回\n");
printf("请输入0--2\n");
scanf("%s",&ch1);
switch(ch1)
{
case '1':findbook();getch();break;
case '2':searchbook();getch();break;
case '0':break;
default:printf("您输入了错误的操作,请重新输入\n");getch();break;
}
}while(ch1!='0');
}
findbook() /*查找图书*/
{
FILE *fp;
char name[10];
int i;
fp=fopen("book.txt","r");
printf("请输入你要查找的书名\n");
scanf("%s",name);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(name,book[i].name)==0)
printf("这本书的详细资料是:序号: %d 书名:%s 作者:%s 出版社: %s 价格:%f \n",book[i].number,book[i].name,book[i].author,book[i].from,book[i].price);
fclose(fp);
return;
}
printf("没有你要查询的书籍\n");
fclose(fp);
return;
}
searchbook()
{
FILE *fp;
char author[10];
int i;
fp=fopen("book.txt","r");
printf("请输入你要查找的作者名\n");
scanf("%s",author);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(author,book[i].author)==0)
printf("这本书的详细资料是:序号: %d 书名:%s 作者:%s 出版社: %s 价格:%f \n",book[i].number,book[i].name,book[i].author,book[i].from,book[i].price);
fclose(fp);
return;
}
printf("没有你要查询的书籍\n");
fclose(fp);
return;
}
changebook() /*修改功能*/
{
FILE *fp,*fp2;
char name[10],ch2;
int i;
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
printf("请你输入要修改的书籍的书字\n");
scanf("%s",name);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(book[i].name,name)==0)
{
printf("你所要修改的书的资料如下,请选择你要修改的内容\n");
printf("序号: %d 书名:%s 作者:%s 出版社: %s 价格:%f \n",book[i].number,book[i].name,book[i].author,book[i].from,book[i].price);
printf("1:修改书的序号\n");
printf("2:修改书名\n");
printf("3:修改作者\n");
printf("4:修改出版社\n");
printf("5:修改价格\n");
printf("6: 返回主菜单\n");
printf("请输入1-6:");
scanf("%s",&ch2);
switch(ch2)
{
case '1':
{
printf("请输入新的序号\n");
scanf("%d",&book[i].number);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '2':
{
printf("请输入新的书名\n");
scanf("%s",book[i].name);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '3':
{
printf("请输入新的作者\n");
scanf("%s",book[i].author);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '4':
{
printf("请输入新的出版社\n");
scanf("%s",book[i].from);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '5':
{
printf("请输入新的价格\n");
scanf("%d",book[i].price);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '6':break;
default:printf("您输入了错误的操作,请重新输入\n");getch();break;
}
continue;
}
fwrite(&book[i],sizeof(struct book),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
{
fwrite(&book[i],sizeof(struct book),1,fp);
}
fclose(fp);
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意键返回\n");
getchar();
return;
}
addbook() /*添加功能*/
{
FILE *fp;
int i=0;
char ch4='y';
fp=fopen("book.txt","a+");
while(ch4=='y'||ch4=='Y')
{
printf("请你输入第%d本书的序号\n",i+1);
scanf("%d",&book[i].number);
printf("请你输入书名\n");
scanf("%s",&book[i].name);
printf("请你输入书的作者\n");
scanf("%s",&book[i].author);
printf("请你输入出版社\n");
scanf("%s",&book[i].from);
printf("请你输入书的价格\n");
scanf("%f",&book[i].price);
fwrite(&book[i],sizeof(struct book),1,fp);
printf("是否要输入下本书\n");
scanf("%s",&ch4);
}
fclose(fp);
}
delbook() /*删除功能*/
{
FILE *fp,*fp2;
int i;
char name[10],ch3;
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
printf("请输入你要删除的书名\n");
printf("如果你输入的书名存在,系统自动删除该信息!如果不存在,系统不做任何改动\n");
scanf("%s",name);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(name,book[i].name)!=0)
fwrite(&book[i],sizeof(struct book),1,fp2);
}
fclose(fp);
fclose(fp2);
printf("是否真的要删除该书籍?删除后该书籍的所有信息将无法恢复《Y/N》\n");
scanf("%s",&ch3);
if(ch3=='y'||ch3=='Y')
{
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
fwrite(&book[i],sizeof(struct book),1,fp);
fclose(fp);
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意键返回\n");
getch();
return;
}
else
{
printf("按任意键返回\n");
getch();
return;
}
}
bookcount() /*统计功能*/
{
FILE *fp;
int i,n=0;
fp=fopen("book.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(book[i].number!=0&&strlen(book[i].name)!=0&&strlen(book[i].author)!=0)
{
printf("第%d本书<序号: %d 书名:%s 作者:%s 出版社: %s 价格:%f \n>\n",i+1,book[i].number,book[i].name,book[i].author,book[i].from,book[i].price);
n=n+1;
}
}
fclose(fp);
printf("目前共有%d本书\n",n);
printf("按任意键\n");
}