众泰sr7性能怎么样:这个题为什么没有循环?

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 13:08:28
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct stud
{
long num;
char name[20];
char sex;
float score;
}student;
main()
{
FILE *fp;
int rec_no;
long offset;
char ch;
if((fp=fopen("d:\\program\\record.dat","rb"))==NULL)
{
printf("\nCannot open file!\n");
getch();
exit(1);
}
do
{
printf("\nplease input recond number:");
scanf("%d",&rec_no);
offset=(rec_no-1)*sizeof(student);
rewind(fp);
if(fseek(fp,offset,0)!=0)
{
printf("can not move there!\n");
exit(1);
}
fread(&student,sizeof(student),1,fp);
printf("num:%ld\n",student.num);
printf("name:%s\n",student.name);
printf("sex(m/f):%c\n",student.sex);
printf("score:%f\n",student.score);
printf("continue(y/n)?");
ch=getchar();getchar();
}while(ch=='y'||ch=='Y');
fclose(fp);
getch();
}
当到最后我输入y时,没有叫我循环输入就退出了,是怎么回事呢?

printf("continue(y/n)?");
ch=getch();
}while((ch=='y')||(ch=='Y'));
fclose(fp);
getch();

程序前面scanf时,你在输入数据后肯定要按一下回车,而回车实际上是ASCII码13和10两个字符,所以后面ch=getchar()时,获得的是ASCII码为10的字符。
建议:在ch=getchar()前加一个getchar();