猜字游戏问题?
这个程序为什么输入字母或字符会进入死循环?如何解决,最好说明一下.是否能简化程序,谢谢!!!程序如下:
#include "math.h"
#include "stdlib.h"
main()
{
int guess,answer[4],i,j,proper,times=0,k;
randomize();
for(i=0;i<4;i++)
{
answer[i]=rand()%10;
for(j=0;j<i;j++)
if(answer[j]==answer[i]) { answer[i]=rand()%10; j=-1; }
}
printf("Please Guess:\n");
while(proper!=4)
{
proper=0;
printf("%d: ",++times);
scanf("%d",&guess);
while(guess<-1 || guess>9999)
{
printf("Input error......\nPlease reenter guess:\n");
scanf("%d",&guess);
}
for(j=3;j>=0;j--)
{
for(i=0;i<4;i++)
if(answer[i]==(int)(guess/pow(10,j)))
if(i+j==3) proper++;
guess=guess-(int)(guess/pow(10,j))*pow(10,j);
}
if(proper==4) printf("Very Good!!\n\nThe number is:");
else printf(" %d correct\n",proper);
}
for(i=0;i<4;i++)
printf("%d",answer[i]);
getch();
}