【发布时间】:2019-11-22 00:50:45
【问题描述】:
我有一个代码,如果用户键入“什么是苹果”,它应该回答“苹果是水果”,但对于这种情况 在文本文件中存储为 [what] 的内容。看起来像这样
[what]
apple = apple is a fruits.
下面是我的代码。它没有打印出“苹果是水果”。我能知道我的错误在哪里吗?
char intent[255];
char key1[255];
char key[255];
char inv[3][10] = { "what","where","who" };
snprintf(intent, sizeof(intent), "[%s]", inv[0]);
printf("%s\n", intent);
scanf("%s",&key1);
if (strcmp (intent == "[what]" && key == "apple") == key1) {
printf ("apple is a fruits");
}
【问题讨论】:
-
这不是你在 C 中比较字符串的方式。
-
嗨,我已经编辑了代码,但它不起作用,并且出现错误“访问冲突读取位置 0x00000000”
-
修改后的代码不应编译——
strcmp()需要两个参数,而您只提供一个。您应该使用strcmp(intent, "[what]") == 0 && strcmp(key, "apple") == 0作为比较两对字符串的条件。 -
你应该从参考中学习,而不是反复试验