【发布时间】:2013-08-11 04:23:53
【问题描述】:
我试图从用户那里获取一个字符串输入,然后根据他们输入的输入运行不同的函数。
例如,假设我问:“你最喜欢的水果是什么?”我希望程序根据他们输入的内容发表评论……我不知道该怎么做。到目前为止,这是我所拥有的:
#include <stdio.h>
#include <string.h>
char fruit[100];
main() {
printf("What is your favorite fruit?\n");
fgets (fruit, 100, stdin);
if (strcmp(fruit, "apple")) {
printf("Watch out for worms!\n");
}
else {
printf("You should have an apple instead.\n");
}
}
当我运行程序时,无论我输入什么,它都不会执行 else 语句。
感谢您的帮助!
【问题讨论】:
-
strcmp返回0(将评估为假)以指示两个字符串相等。 -
阅读您尝试使用的函数的文档是否真的有害?
-
另外,不要使用
main(),而是使用int main(void)。