我有这个应该是'聊天模拟器'的程序,它现在唯一要做的就是回复'你好!'当用户输入'Hello'时。有没有可能的方式来在strcmp中使用char?
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
int main()
{
printf("Chat simulator!\n");
do {
char x;
printf("[User1] ");
scanf("%s",&x);
if (strcmp (x,"Hello") == 0)
{
Sleep(1500);
printf("[User2] Hello!\n");
}
else {}
} while(1);
}
我知道strcmp
仅供const char *
,不是一个单一的char
,这里这就是问题所在,但我无法找到任何相关的解决方案,因为我需要在scanf
使用char x
,所以它不能是const char *
。
也有可能我使用的是strcmp
错误。
代码:块警告:
passing argument 1 of 'strcmp' makes pointer from integer without a cast*
expected 'const char *' but argument is of type 'char'*
编辑:
所以我改变了char
到为@ robin.koch告诉我,一切正常,因为它应该。谢谢!
为什么你尝试将用户的信息(整个报文)存储'char'里面? –
问题不在于你认为的地方。 'x'是一个单独的'char',而你试图将一个字符串(字符数组)填入它。 –
x只有一个字符。您正试图存储一个字符串(多个字符)。改用char数组。 char x [16];例如。 –