问题遇到的现象和发生背景
一个猜数的小程序,不明白主函数中这样写为什么循环只能执行一次
问题相关代码,请勿粘贴截图
#include<stdio.h>
#include<time.h>
#define N 100
int random_number(void);
int guess_interface(void);
int main() {
char i;
printf("A new number has been choose\n");
do {
guess_interface();
printf("Play again?\n");
scanf("%c", &i);
} while (i == 'Y');
return 0;
}
int guess_interface(void) {
int num, guess, fre=1;
num = random_number();
printf("Enter guess:");
scanf_s("%d", &guess);
while (guess != num) {
if (guess < num) {
printf("Too low; Try again");
fre++;
}
else if (guess > num) {
printf("Too high; Try again");
fre++;
}
scanf_s("%d", &guess);
}
printf("You win in %d guess\n", fre);
return 0;
}
int random_number(void) {
srand((unsigned)time(NULL));
return rand()%N+1;
}