| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1186 人关注过本帖, 1 人收藏
标题:[求助]小弟在编图书管理系统,scanf中的问题~
只看楼主 加入收藏
chrisvvs
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-6-2
收藏(1)
 问题点数:0 回复次数:5 
[求助]小弟在编图书管理系统,scanf中的问题~

小弟在做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");
}

搜索更多相关主题的帖子: 图书管理系统 scanf 在编 int 
2007-06-02 11:56
yaguang_li
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-5-11
收藏
得分:0 
double temp;
printf("请你输入书的价格\n");
scanf("%f",&temp);
book[i].price=temp;
这样应该就可以了
2008-09-16 23:17
forever74
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:CC
等 级:版主
威 望:58
帖 子:1705
专家分:4345
注 册:2007-12-27
收藏
得分:0 
啊哈,我等待有人问这个问题已经很久了
这个是TC天生的BUG
换别的开发环境就好了
2008-09-16 23:31
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
你只需要载入math.h,并调用一个无关的浮点函数就可以了。当然你不调用也可以试试看。TC为了节约内存,默认是不载入浮点模块的,有时候它的自动机制检测不出来你在使用浮点数,就需要你自己去想办法打开它……

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-09-17 02:05
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
帮LZ格式化一下:

程序代码:
#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");
}

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-09-17 02:07
yqdrrjhan
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-7-20
收藏
得分:0 
版主很热心,支持~~~

2008-09-17 10:14
快速回复:[求助]小弟在编图书管理系统,scanf中的问题~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020952 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved