我找到了一个不错的使用strcmp的示例,但它只适用于fgets(),我需要使其适用于scanf。所以,这是代码:
int main(void) {
char fruit[] = "apple\n";
char ans[80];
do {
printf ("Guess my favorite fruit? ");
scanf ("%s",ans);
} while (strcmp (fruit, ans) != 0);
puts ("Correct answer!");
return 0;
}
即使我写了正确的答案(“apple”),它仍然停留在循环中,并继续问我最喜欢的水果是什么...我猜这与未在ans [80]中编写的字符有关(我需要将其作为最多具有80个字符的字符数组)。我不理解这一点...
提前致谢。