我正在尝试编写一个程序,让用户输入一个单词或字符,存储它,然后打印出来,直到用户再次输入相同的内容,退出程序。我的代码如下:
#include <stdio.h>
int main()
{
char input[40];
char check[40];
int i = 0;
printf("Hello!\nPlease enter a word or character:\n");
gets(input); /* obsolete function: do not use!! */
printf("I will now repeat this until you type it back to me.\n");
while (check != input)
{
printf("%s\n", input);
gets(check); /* obsolete function: do not use!! */
}
printf("Good bye!");
return 0;
}
问题是,即使用户输入(
check
)与原始输入(input
)匹配,我仍然不断打印输入字符串。我是否比较这两个值的方式有误?
gets( )
已经从标准中移除,请改用fgets( )
。新函数可以实现同样的功能,但更安全可靠。 - lost_in_the_sourcegets()
是不可取的。自C11标准以来,它也已被删除 -> 请阅读为什么gets()函数如此危险,不应该使用? - RobertS supports Monica Cellio