为什么 主函数 中的getchar函数不显示啊!!!
这个代码是用来随机出现加减乘除4种体型的。输入N退出。然后显示 每种题目出现的次数 和 做对的次数。现在问题是,出现If you want to exit the quit,press 'N' !! Or press any other keys.之后,就直接在进入循环,不等待我getchar,这是为什么?
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int random(int a,int b);
void add(int m,int n,int a[],int b[]);
void sub(int m,int n,int a[],int b[]);
void mul(int m,int n,int a[],int b[]);
void divi(int m,int n,int a[],int b[]);
void main()
{
int m,n,i;
int total[4]={0},right[4]={0};
char c;
srand((unsigned)time(NULL));
do
{
m=random(1,100);n=random(1,100);
i=random(1,4);
switch(i)
{
case(1):add(m,n,total,right);break;
case(2):sub(m,n,total,right);break;
case(3):mul(m,n,total,right);break;
case(4):divi(m,n,total,right);break;
}
printf("If you want to exit the quit,press 'N' !! Or press any other keys.\n\n");
c=getchar();
fflush(stdin);
}
while(c!='N');
printf("\nYou have done %d addition, right in %d.\n",total[0],right[0]);
printf("You have done %d subtraction, right in %d.\n",total[1],right[1]);
printf("You have done %d multipliaction, right in %d.\n",total[2],right[2]);
printf("You have done %d divition, right in %d.\n",total[3],right[3]);
}
/************* 播种 *************/
int random(int a,int b)
{
int i;
i=rand()%(b-a+1)+a;
return(i);
}
/************* 加法 *************/
void add(int m,int n,int x[],int y[])
{
int a;
x[0]++;
printf("%d+%d=",m,n);
scanf("%d",&a);
if(m+n==a)
{printf("Great\n"); y[0]++;}
else printf("It doesn't matter,try another\n");
}
/************* 减法 *************/
void sub(int m,int n,int x[],int y[])
{
int a,i,j;
x[1]++;
printf("%d-%d=",i=m>n?m:n,j=m<n?m:n);
scanf("%d",&a);
if(i-j==a)
{printf("Great\n"); y[1]++;}
else printf("It doesn't matter,try another\n");
}
/************* 乘法 *************/
void mul(int m,int n,int x[],int y[])
{
int a;
x[2]++;
printf("%d*%d=",m,n);
scanf("%d",&a);
if(m*n==a)
{printf("Great\n"); y[2]++;}
else printf("It doesn't matter,try another\n");
}
/************* 除法 *************/
void divi(int m,int n,int x[],int y[])
{
int a;
x[3]++;
while(m%n!=0)
{
m=random(1,100);n=random(1,100);
}
printf("%d÷%d=",m,n);
scanf("%d",&a);
if(m/n==a)
{printf("Great\n"); y[3]++;}
else printf("It doesn't matter,try another\n");
}
[ 本帖最后由 smile_line 于 2013-5-26 18:47 编辑 ]