c语言--猜数游戏(输入错误字符时处理)

#include

#include

#include

int main()

{

int magic;

int ret;

int guess;

int counter = 0;

srand(time(NULL));

magic = rand() % 100 + 1;

do{

printf("please guess a magic numbre:");

ret = scanf("%d",&guess);

while(ret ! = 1)

{

while(getchar() ! = '\n');

printf("please guess a magic number:");

ret = scanf("%d",&guess);

}

counter++;

if(guess>magic)

printf("Wrong! Too big!\n");

else if(guess

printf("Wrong! Too small!\n");

else

printf("Right!\n");

}while(guess ! = magic && counter < 10);

printf("counter = %d\n",counter);

return 0;

}

你可能感兴趣的:(c语言)