关于scanf(" %c"&select)与scanf("%c"&select)解决办法
关于scanf(" %c",&select)与scanf("%c",&select)
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#define N 8
int main(void)
{
typedef struct
{
int num;
char name[18];
char sex[3];
int age;
}StudentType;
StudentType student;
StudentType aStudent[N]=
{
{200301,"张学友","男",43},
{200302,"刘德华","男",42},
{200303,"郭富城","男",45},
{200304,"黎天王","男",41},
{200305,"张靓颖","女",23},
{200306,"李宇春","女",22},
{200307,"窦文涛","男",43},
{200308,"孔夫子","男",36},
};
char fileName[80];
FILE *pFile;
int i;
char select='y';
printf("输入文件名:");
gets(fileName);
if((pFile=fopen(fileName,"wb+"))==NULL)
{
printf("打开文件%s失败!\n""敲任意键退出!\n",fileName);
system("pause");
exit(1);
}
fwrite(&aStudent,sizeof(StudentType),N,pFile);
while(select=='y')
{
rewind(pFile);
printf("输入要显示的学生序号(1~%d):",N);
scanf("%d",&i);
assert(i>=1&&i<=N);
fseek(pFile,sizeof(StudentType)*(i-1),SEEK_CUR);
fread(&student,sizeof(StudentType),1,pFile);
printf("\n学号:%d\n姓名:%s\n性别:%s\n年龄:%d\n\n",student.num,student.name,student.sex,student.age);
printf("是否继续(Y/N):");
scanf("%c",&select);
select=tolower(select);
assert(select=='y'||select=='n');
}
fclose(pFile);
system("pause");
return 0;
}
这里scanf(" %c",&select),如若没有空格即:scanf("%c",&select),程序将会由assert结束
执行结果:
调用scanf()结束:将不会进行输入,直接进行后面的运算,导致assert,退出程序。
新手,呵呵,自己手动调试结果: 在调用scanf()前:&select 值:0x0012fe04 "y烫?" select 值: 121 'y'
调用scanf()后:&select 值:0x0012ffe04 "烫?" select 值:10 ''
这里就糊涂了,scanf() 对这种情况为什么会有这种结果,scanf()处理这种情况的详细细节。
新人分不多,求高手解析,拜谢!!
------解决方案--------------------
由于C一般没有把最后一个回车键处理掉,而是停留在缓冲区内。从而导致你scanf的时候获得到了回车键。参考下代码。
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#define N 8
int main(void)
{
typedef struct
{
int num;
char name[18];
char sex[3];
int age;
}StudentType;
StudentType student;
StudentType aStudent[N]=
{
{200301,"张学友","男",43},
{200302,"刘德华","男",42},
{200303,"郭富城","男",45},
{200304,"黎天王","男",41},
{200305,"张靓颖","女",23},
{200306,"李宇春","女",22},
{200307,"窦文涛","男",43},
{200308,"孔夫子","男",36},
};
char fileName[80];
FILE *pFile;
int i;
char select='y';
printf("输入文件名:");
gets(fileName);
if((pFile=fopen(fileName,"wb+"))==NULL)
{
printf("打开文件%s失败!\n""敲任意键退出!\n",fileName);
system("pause");
exit(1);
}
fwrite(&aStudent,sizeof(StudentType),N,pFile);
while(select=='y')
{
rewind(pFile);
printf("输入要显示的学生序号(1~%d):",N);
scanf("%d",&i);
assert(i>=1&&i<=N);
fseek(pFile,sizeof(StudentType)*(i-1),SEEK_CUR);
fread(&student,sizeof(StudentType),1,pFile);
printf("\n学号:%d\n姓名:%s\n性别:%s\n年龄:%d\n\n",student.num,student.name,student.sex,student.age);
printf("是否继续(Y/N):");
scanf("%c",&select);
select=tolower(select);
assert(select=='y'||select=='n');
}
fclose(pFile);
system("pause");
return 0;
}
这里scanf(" %c",&select),如若没有空格即:scanf("%c",&select),程序将会由assert结束
执行结果:
调用scanf()结束:将不会进行输入,直接进行后面的运算,导致assert,退出程序。
新手,呵呵,自己手动调试结果: 在调用scanf()前:&select 值:0x0012fe04 "y烫?" select 值: 121 'y'
调用scanf()后:&select 值:0x0012ffe04 "烫?" select 值:10 ''
这里就糊涂了,scanf() 对这种情况为什么会有这种结果,scanf()处理这种情况的详细细节。
新人分不多,求高手解析,拜谢!!
------解决方案--------------------
由于C一般没有把最后一个回车键处理掉,而是停留在缓冲区内。从而导致你scanf的时候获得到了回车键。参考下代码。
- C/C++ code
#define N 8 int main(void) { typedef struct { int num; char name[18]; char sex[3]; int age; }StudentType; StudentType student; StudentType aStudent[N]= { {200301,"张学友","男",43}, {200302,"刘德华","男",42}, {200303,"郭富城","男",45}, {200304,"黎天王","男",41}, {200305,"张靓颖","女",23}, {200306,"李宇春","女",22}, {200307,"窦文涛","男",43}, {200308,"孔夫子","男",36}, }; char fileName[80]; FILE *pFile; int i; char select='y'; printf("输入文件名:"); gets(fileName); if((pFile=fopen(fileName,"wb+"))==NULL) { printf("打开文件%s失败!\n""敲任意键退出!\n",fileName); system("pause"); exit(1); } fwrite(&aStudent,sizeof(StudentType),N,pFile); while(select=='y') { rewind(pFile); printf("输入要显示的学生序号(1~%d):",N); scanf("%d",&i); getchar(); assert(i>=1&&i<=N); fseek(pFile,sizeof(StudentType)*(i-1),SEEK_CUR); fread(&student,sizeof(StudentType),1,pFile); printf("\n学号:%d\n姓名:%s\n性别:%s\n年龄:%d\n\n",student.num,student.name,student.sex,student.age); printf("是否继续(Y/N):"); scanf("%c",&select); select=tolower(select); assert(select=='y'||select=='n'); } fclose(pFile); system("pause"); return 0; }