一个简单的猜数字游戏的字符读取问题
一个简单的猜数字游戏,一次猜字完成后读取一个判断是否继续玩的字符时,总是读取了输入最后一个答案时候的回车输入而不是等待新的输入.怎么解决.大家运行一下就明白怎么回事?猜一次.#include<stdio.h>
#include<time.h>
#include<stdlib.h>
char p;
void caishu(void)
{
int m,n;
srand(time(NULL));
m=rand()%1000+1;
printf("I havu a number between 1 and 1000.\nCan you guess my number.\nPlease type your frist guess.\n");
scanf("%d",&n);
while(n!=m){
if(n>m){
printf("Too high.Try again.\n");
scanf("%d",&n);
}
if(n<m){
printf("Too low.Try again.\n");
scanf("%d",&n);
}
}
printf("Excellent!You guessed the number!\nWould you like to play again?( y or n ):");
scanf("%c",&p);/*就是这里了,总是自动读取了最后一个正确答案的回车,而不是等待输入*/
}
main()
{
caishu();
while(p=='y'){
caishu();
}
if(p=='n')
printf("Byebye");
while(p!='y'&&p!='n'){
printf("Wrong order\n");
printf("Would you play again?( y or n ):");
scanf("%c",&p);
while(p=='y'){
caishu();
}
if(p=='n')
printf("Byebye");
}
return 0;
}
[此贴子已经被作者于2007-3-28 21:14:31编辑过]