【发布时间】:2014-03-05 01:01:08
【问题描述】:
我的代码是
struct bat
{
char name[20];
int runs;
}b1;
void main()
{
char ch='y';
FILE *p;
p=fopen("details.dat","wb");
if(p==NULL)
{
printf("unable");
exit(0);
}
else
{
while ( ch == 'y' )
{
printf("enter details");
scanf("%s%d",b1.name,&b1.runs);
fwrite(&b1,sizeof(b1),1,p);
printf("do you want to eneter more");
scanf("%c",&ch);//it is not working
}
}
return 0;
}
如果我使用 getche() 代替 scanf,则代码可以完美运行并具有所需的输出。 但为什么它不能与 scanf() 一起使用?
【问题讨论】:
-
您声明了
char c = 'y',但您的其余代码引用了ch。这是一个错字吗?ch是否在其他地方定义,而不是初始化为y? -
"%c"改为" %c"