| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1190 人关注过本帖
标题:[求助]关于scanf不能正常赋值
只看楼主 加入收藏
兰风清寒
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-10-14
收藏
 问题点数:0 回复次数:10 
[求助]关于scanf不能正常赋值

我为编写c语言的作业的数据保存,编写了一个小测试程序,但这个程序遇到问题,scanf不能正常赋值,当将scanf("%c",&t);改为t=getch();后这儿过去了,后面整型和float型又过不去,最主要的,不知道为什么会这样!!
#include<stdio.h>
#include<stdlib.h>

void write(FILE *fp);
void read(FILE *fp);

struct h{
char a;
int b;
long c;
struct h *next;
};
struct h *head;

void main (void)
{
int t,n;
struct h *p,*head;
int flag=1;
FILE *fp;

printf("Is this a new set file?(Y/N)");
scanf("%c",&t);

printf("Hello world 10");
if (t=='y'&&t=='Y')
{
printf("Hello world");
if((fp=fopen("mf","r"))==NULL) {
printf("Can't open file mf");
exit(-1);
}

printf("Hello world 1");
read(fp);
printf("Hello world 2");
}
else
{ printf("Hello world5");
if((fp=fopen("mf","r"))==NULL) {
printf("Can't open file mf");
exit(-1);
}
}

printf("Hello world");

/*建立链表*/
while(flag){
p=(struct h *)malloc(sizeof(struct h));
printf("input:\t char a\tint b\tlong c\n");
scanf("%c%d%l",&p->a,&p->b,&p->c);
p->next=NULL;
printf("input 0 to stop inputing!");
scanf("%d",&flag);
} /*end of while*/

/*保存数据*/
write(fp);

}


void write(FILE *fp)
{
struct h *p;

for(p=head;p!=NULL;p=p->next)
fwrite(p,sizeof(struct h),1,fp);
}


void read(FILE *fp)
{
struct h *p,*tail,buf;

fread(&buf,sizeof(struct h),1,fp);
p=(struct h *)malloc(sizeof(struct h));
*p=buf;head=p;
while(p!=NULL){
fread(&buf,sizeof(struct h),1,fp);
p=(struct h *)malloc(sizeof(struct h));
tail=p;*p=buf;
tail->next=p;
}
tail->next=p;
}

哪位高手帮忙看一下啊?!

搜索更多相关主题的帖子: 赋值 scanf void struct int 
2006-10-16 21:49
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
以下是引用兰风清寒在2006-10-16 21:49:11的发言:

我为编写c语言的作业的数据保存,编写了一个小测试程序,但这个程序遇到问题,scanf不能正常赋值,当将scanf("%c",&t);改为t=getch();后这儿过去了,后面整型和float型又过不去,最主要的,不知道为什么会这样!!
#include<stdio.h>
#include<stdlib.h>

void write(FILE *fp);
void read(FILE *fp);

struct h{
char a;
int b;
long c;
struct h *next;
};
struct h *head;

void main (void)
{
int t,n;
struct h *p,*head;
int flag=1;
FILE *fp;

printf("Is this a new set file?(Y/N)");
scanf("%c",&t);

printf("Hello world 10");
if (t=='y'&&t=='Y')//这个怎么可能能做到.
{
printf("Hello world");
if((fp=fopen("mf","r"))==NULL) {
printf("Can't open file mf");
exit(-1);
}

printf("Hello world 1");
read(fp);
printf("Hello world 2");
}
else
{ printf("Hello world5");
if((fp=fopen("mf","r"))==NULL) {
printf("Can't open file mf");
exit(-1);
}
}

printf("Hello world");

/*建立链表*/
while(flag){
p=(struct h *)malloc(sizeof(struct h));
printf("input:\t char a\tint b\tlong c\n");
scanf("%c%d%l",&p->a,&p->b,&p->c); //因为前面有个回车,在这里被p->a接收.
p->next=NULL;
printf("input 0 to stop inputing!");
scanf("%d",&flag);
} /*end of while*/

/*保存数据*/
write(fp);

}


void write(FILE *fp)
{
struct h *p;

for(p=head;p!=NULL;p=p->next)
fwrite(p,sizeof(struct h),1,fp);
}


void read(FILE *fp)
{
struct h *p,*tail,buf;

fread(&buf,sizeof(struct h),1,fp);
p=(struct h *)malloc(sizeof(struct h));
*p=buf;head=p;
while(p!=NULL){
fread(&buf,sizeof(struct h),1,fp);
p=(struct h *)malloc(sizeof(struct h));
tail=p;*p=buf;
tail->next=p;
}
tail->next=p;
}

哪位高手帮忙看一下啊?!


倚天照海花无数,流水高山心自知。
2006-10-16 21:56
编程新贵
Rank: 1
等 级:新手上路
帖 子:98
专家分:0
注 册:2006-8-11
收藏
得分:0 
主要是字符留在了缓冲区里了,所以不能正常输入了
用语句fflush(stdin);来清除缓冲区里面的字符就能正常输入了
2006-10-16 22:16
兰风清寒
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-10-14
收藏
得分:0 

还别说,fflush(stdin);早就用了,没用啊!!我还曾经传给同学,在他的电脑上同样不能运行!
还有,t改成char也没用;

但是以下的程序又能正常运行:
#include<stdio.h>

void main (void)
{
char t;

printf("Is this a new set file?(Y/N)");
printf("%c",t);
scanf("%c",&t);
printf("Hello world 10");

printf("%c",t);
if (t=='y'||t=='Y')
{
printf("Hello world");
}
}

2006-10-16 22:35
编程新贵
Rank: 1
等 级:新手上路
帖 子:98
专家分:0
注 册:2006-8-11
收藏
得分:0 
你试试将浮点运算打开,随便定义一个float a;a++;
2006-10-16 22:43
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 

可能是使用文件的原因.


倚天照海花无数,流水高山心自知。
2006-10-16 22:44
编程新贵
Rank: 1
等 级:新手上路
帖 子:98
专家分:0
注 册:2006-8-11
收藏
得分:0 
scanf("%c%d%l",&p->a,&p->b,&p->c);

这个接收最好不要写在一行

2006-10-16 23:03
兰风清寒
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-10-14
收藏
得分:0 

我将原程序改为下面形式,可以正常运行,
先在这里谢谢各位大侠!!
——看来是程序读取数据出错!

——那么文件该怎么读取啊?特别对于十字链表!
——我的NULL是不是用多了?或者根本就不能这样用作判断的条件?

请各位大侠再次拔刀相助!
为我排解疑惑,在下感激不胜!
#include<stdio.h>
#include<stdlib.h>

void write(FILE *fp);
void read(FILE *fp);

struct h{
char a;
int b;
long c;
struct h *next;
};
struct h *head;

void main (void)
{
char t;
struct h *p,*head;
int flag=1,n;
FILE *fp;

printf("Is this a new set file?(Y/N)");
scanf("%*c%c",&t);
/*scanf("%c",&t);*/


printf("Hello world\n");

/*建立链表*/
while(flag){
p=(struct h *)malloc(sizeof(struct h));
printf("input:\t char a\tint b\tlong c\n");
scanf("%*c%c%d%ld",&p->a,&p->b,&p->c);
p->next=NULL;
printf("input 0 to stop inputing!");
scanf("%d",&flag);
} /*end of while*/

/*保存数据*/
write(fp);

}


void write(FILE *fp)
{
struct h *p;

for(p=head;p!=NULL;p=p->next)
fwrite(p,sizeof(struct h),1,fp);
}

2006-10-17 15:35
编程新贵
Rank: 1
等 级:新手上路
帖 子:98
专家分:0
注 册:2006-8-11
收藏
得分:0 
以下是引用兰风清寒在2006-10-17 15:35:48的发言:

我将原程序改为下面形式,可以正常运行,
先在这里谢谢各位大侠!!
——看来是程序读取数据出错!

——那么文件该怎么读取啊?特别对于十字链表!
——我的NULL是不是用多了?或者根本就不能这样用作判断的条件?

请各位大侠再次拔刀相助!
为我排解疑惑,在下感激不胜!
#include<stdio.h>
#include<stdlib.h>

void write(FILE *fp);
void read(FILE *fp);

struct h{
char a;
int b;
long c;
struct h *next;
};
struct h *head;

void main (void)
{
char t;
struct h *p,*head;
int flag=1,n;
FILE *fp;

printf("Is this a new set file?(Y/N)");
scanf("%*c%c",&t); //这句什么意思能说下吗
/*scanf("%c",&t);*/


printf("Hello world\n");

/*建立链表*/
while(flag){
p=(struct h *)malloc(sizeof(struct h));
printf("input:\t char a\tint b\tlong c\n");
scanf("%*c%c%d%ld",&p->a,&p->b,&p->c);
p->next=NULL;
printf("input 0 to stop inputing!");
scanf("%d",&flag);
} /*end of while*/

/*保存数据*/
write(fp);

}


void write(FILE *fp)
{
struct h *p;

for(p=head;p!=NULL;p=p->next)
fwrite(p,sizeof(struct h),1,fp);
}

2006-10-17 16:10
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
scanf("%*c%c",&t); //这句什么意思能说下吗
应该是跳过1字符再接收字符.

倚天照海花无数,流水高山心自知。
2006-10-17 16:19
快速回复:[求助]关于scanf不能正常赋值
数据加载中...
 
   



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

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