C中的Strcmp不工作

C中的Strcmp不工作,c,strcmp,C,Strcmp,我是C语言新手,下面的程序有问题。它询问人们是否健康,并相应地显示一条消息。到目前为止,我已经尝试使用!strcmp、strcmp和strncmp,如果if,则无一返回正值,则全部跳到else语句。谁能告诉我哪里出了问题,因为语法对我来说很好 #include "stdafx.h" #include <stdio.h> #include <string.h> int main() { char well[3]; printf("Hello, are you

我是C语言新手,下面的程序有问题。它询问人们是否健康,并相应地显示一条消息。到目前为止,我已经尝试使用
!strcmp
strcmp
strncmp
,如果
if
,则无一返回正值,则全部跳到
else
语句。谁能告诉我哪里出了问题,因为语法对我来说很好

#include "stdafx.h"
#include <stdio.h>
#include <string.h>

int main()
{
   char well[3];

   printf("Hello, are you well today? (Yes/No) ");
   scanf_s ("%s",well);
       if (!strcmp(well,"Yes")){
           printf("Glad to hear it, so am I\n");
   }
   else{
       printf("Sorry to hear that, I hope your day gets better\n");
   }   
system("PAUSE");   
return 0;
}
#包括“stdafx.h”
#包括
#包括
int main()
{
焦井[3];
printf(“你好,你今天好吗?(是/否)”);
scanf_s(“%s”,井);
如果(!strcmp(好的,“是”)){
printf(“很高兴听到这个消息,我也很高兴”);
}
否则{
printf(“很抱歉听到这个消息,我希望你的日子过得更好”\n”);
}   
系统(“暂停”);
返回0;
}
非常感谢大家给出的所有答案,不幸的是,它们似乎都不起作用。分配一个4来考虑null并没有什么区别。调用scanf而不是scanf_s会导致访问冲突(这很奇怪,因为程序的其余部分使用普通scanf。向scanf_s添加'4'参数也没有什么区别。在这里,我真的很高兴在这行末尾容纳空值,但程序似乎无法识别它。

字符串
“是”
包含一个空终止符,在内存中看起来像
{'Y'、'e'、's'、'\0'}
。因此它需要4个字符,因此无法安全地读入3元素
char
数组,因此您应该至少给出4个元素

char well[4];
char well[4];
或者,正如伦丁所建议的那样

#define MAX_RESPONSE (sizeof("Yes"))
char well[MAX_RESPONSE];
paxdiablo解释说您也没有正确调用
sscanf\s
。这里一个可能的修复方法是切换到调用
scanf
,传递最大字符串大小并检查用户输入

while (scanf(" %3s", well) != 1);
当然,坚持使用
scanf\u s

while (scanf_s(" %s", well, sizeof(well)-1) != 1);
字符串
“Yes”
包含一个空终止符,在内存中看起来像
{'Y'、'e'、's'、'\0'}
。因此它需要4个字符,因此无法安全地读入3元素
字符
数组,因此您应该至少给出4个元素

char well[4];
char well[4];
或者,正如伦丁所建议的那样

#define MAX_RESPONSE (sizeof("Yes"))
char well[MAX_RESPONSE];
paxdiablo解释说您也没有正确调用
sscanf\s
。这里一个可能的修复方法是切换到调用
scanf
,传递最大字符串大小并检查用户输入

while (scanf(" %3s", well) != 1);
当然,坚持使用
scanf\u s

while (scanf_s(" %s", well, sizeof(well)-1) != 1);

首先,您在
井中没有足够的空间来存储3个字符和一个空终止符


scanf_s
是安全的,因为它要求(请参见)某些格式字符串,如
s
具有长度,而您缺少长度。

首先,您在
井中没有足够的空间来存储3个字符和一个空终止符


scanf\u s
是安全的,因为它要求(请参阅)某些格式字符串,如
s
具有您缺少的长度。

为终止的
null
字符分配
4个字节
。这解决了您的问题


为终止的
null
字符分配
。这解决了您的问题

scanf_s要求字符串的长度:

scanf_s ("%s", well, 4);

scanf_s要求字符串的长度:

scanf_s ("%s", well, 4);


每个存储字符串的字符数组都有一个附加字符
\0
,用作字符串终止符。它表示字符串的结尾。因此
“Yes”
需要一个大小为
sizeof(char)的数组*4要存储的
。在您的程序中,只有前3个字符存储在
中。因此
strcmp()
返回一个非零数字,因此
!strcmp
始终是
0
,因此控件总是跳转到
else
。以下是工作代码:

#include "stdafx.h"
#include <stdio.h>
#include <string.h>

int main()
{
   char well[4];// "Yes" needs sizeof(char)*4, to account for the end `\0`

   printf("Hello, are you well today? (Yes/No) ");
   scanf ("%s",well);      //Use scanf(),scanf_s() works for Uncle Gates only

       if (!strcmp(well,"Yes"))
       printf("Well...glad to hear it, so am I\n");

       else
       printf("Sorry to hear that,umm....can I take your wife out?\n");

system("PAUSE");   //This works for Uncle Gates only (not portable)
return 0;
}
#包括“stdafx.h”
#包括
#包括
int main()
{
char well[4];/“Yes”需要sizeof(char)*4来解释结尾“\0”`
printf(“你好,你今天好吗?(是/否)”);
scanf(“%s”,好);//使用scanf(),scanf_s()仅适用于盖茨叔叔
如果(!strcmp(好的,“是”))
printf(“很高兴听到这个消息,我也很高兴”);
其他的
printf(“很抱歉听到这个,嗯……我可以带你妻子出去吗?\n”);
系统(“暂停”);//这只适用于盖茨叔叔(不可移植)
返回0;
}

存储字符串的每个字符数组都有一个附加字符
\0
,用作字符串终止符。它表示字符串的结尾。因此
“Yes”
需要一个大小为
sizeof(char)的数组*4要存储的
。在您的程序中,只有前3个字符存储在
中。因此
strcmp()
返回一个非零数字,因此
!strcmp
始终是
0
,因此控件总是跳转到
else
。以下是工作代码:

#include "stdafx.h"
#include <stdio.h>
#include <string.h>

int main()
{
   char well[4];// "Yes" needs sizeof(char)*4, to account for the end `\0`

   printf("Hello, are you well today? (Yes/No) ");
   scanf ("%s",well);      //Use scanf(),scanf_s() works for Uncle Gates only

       if (!strcmp(well,"Yes"))
       printf("Well...glad to hear it, so am I\n");

       else
       printf("Sorry to hear that,umm....can I take your wife out?\n");

system("PAUSE");   //This works for Uncle Gates only (not portable)
return 0;
}
#包括“stdafx.h”
#包括
#包括
int main()
{
char well[4];/“Yes”需要sizeof(char)*4来解释结尾“\0”`
printf(“你好,你今天好吗?(是/否)”);
scanf(“%s”,好);//使用scanf(),scanf_s()仅适用于盖茨叔叔
如果(!strcmp(好的,“是”))
printf(“很高兴听到这个消息,我也很高兴”);
其他的
printf(“很抱歉听到这个,嗯……我可以带你妻子出去吗?\n”);
系统(“暂停”);//这只适用于盖茨叔叔(不可移植)
返回0;
}

将4个字节分配给well,然后将memset设置为NULL,然后再试一次

char well[4];
memset(well,0,4)

将4个字节分配给well,然后将memset分配给NULL,然后再试一次

char well[4];
memset(well,0,4)

或者更好的是,不要在代码中使用奇怪的幻数。
#定义最坏情况(sizeof(“Yes”))
字符井[最坏情况];
@Lundin很好,我已经按照建议更新了答案。谢谢你的建议。不幸的是,尝试建议似乎没有任何影响。@user2346526我的
scanf
建议中有几个错误。我已经更正了