读取字符串并与特定值进行比较

时间:2022-04-17 20:22:26

I want to read a string from a console application and compare it:

我想从控制台应用程序中读取一个字符串并进行比较:

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

char* answer;

int _tmain(int argc, _TCHAR* argv[])
{
   printf("(yes/no):");
   scanf("%s", &answer);
   if (answer=="yes")
   {
      printf("Yes");
   }
   else
   {
      printf("Exiting...");
   }
   return 0;
}

I always get the message Exiting... when I put yes. How is it possible that I read the correct value of yes but it is not detected by the comparison - answer=="yes" -?

我总是得到消息退出...当我放肯定时。我怎么可能读到正确的yes值,但是比较没有检测到 - 答案==“是” - ?

Also tried this:

还试过这个:

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

    char answer[100];

    int _tmain(int argc, _TCHAR* argv[])
    {
       printf("(yes/no):");
       scanf("%s", answer);
       if (!strcmp(answer,"yes"))
       {
          printf("Yes");
       }
       else
       {
          printf("Exiting...");
       }
       return 0;
    }

this led me to the 2nd option of "Exiting..." as well. What is the error here?

这让我成为了“退出......”的第二个选择。这里有什么错误?

2 个解决方案

#1


5  

Many problems in your code

你的代码中有很多问题

1) You did not allocate memory for answer. Do

1)你没有为回答分配内存。做

answer = malloc(100);

Do not forget to free(answer) it later though.

不要忘记稍后释放(回答)它。

OR

You can also use arrays directly since you do not need dynamic memory

您也可以直接使用数组,因为您不需要动态内存

char answer[100];


2) You need to char * to printf() not char **. Do

scanf("%s", answer);


3) Use strcmp to compare strings, not ==.

if (!strcmp(answer, "yes")
{
    printf("Yes");
}

! is there because, strcmp returns 0 when the strings match.

!因为,strcmp在字符串匹配时返回0。


4) You should also check for return values, like here for scanf().

if (scanf("%s", answer) != 1)
{
    printf("scanf failed");
    exit(0);
}


5) You should also mention the number of chars to be read by scanf() to avoid buffer overflow.

scanf("%99s", answer)

For a char array[100] of size 100, one should give 99 to keep place for the null character \0.

对于大小为100的char数组[100],应该给99保留空字符\ 0的位置。

#2


0  

To compare strings, you should use strcmp().

要比较字符串,您应该使用strcmp()。

That said, answer is not allocated memory. You need to either use an array or use dynamic memory allocation to get the proper memory allocated to this before you can actually use answer.

也就是说,答案没有分配内存。在实际使用answer之前,您需要使用数组或使用动态内存分配来获取分配给它的正确内存。

Basically, you scan statement should look like

基本上,你扫描语句应该是这样的

scanf("%s", answer);

with proper memory allocated to answer beforehand.

分配适当的内存以事先回答。

#1


5  

Many problems in your code

你的代码中有很多问题

1) You did not allocate memory for answer. Do

1)你没有为回答分配内存。做

answer = malloc(100);

Do not forget to free(answer) it later though.

不要忘记稍后释放(回答)它。

OR

You can also use arrays directly since you do not need dynamic memory

您也可以直接使用数组,因为您不需要动态内存

char answer[100];


2) You need to char * to printf() not char **. Do

scanf("%s", answer);


3) Use strcmp to compare strings, not ==.

if (!strcmp(answer, "yes")
{
    printf("Yes");
}

! is there because, strcmp returns 0 when the strings match.

!因为,strcmp在字符串匹配时返回0。


4) You should also check for return values, like here for scanf().

if (scanf("%s", answer) != 1)
{
    printf("scanf failed");
    exit(0);
}


5) You should also mention the number of chars to be read by scanf() to avoid buffer overflow.

scanf("%99s", answer)

For a char array[100] of size 100, one should give 99 to keep place for the null character \0.

对于大小为100的char数组[100],应该给99保留空字符\ 0的位置。

#2


0  

To compare strings, you should use strcmp().

要比较字符串,您应该使用strcmp()。

That said, answer is not allocated memory. You need to either use an array or use dynamic memory allocation to get the proper memory allocated to this before you can actually use answer.

也就是说,答案没有分配内存。在实际使用answer之前,您需要使用数组或使用动态内存分配来获取分配给它的正确内存。

Basically, you scan statement should look like

基本上,你扫描语句应该是这样的

scanf("%s", answer);

with proper memory allocated to answer beforehand.

分配适当的内存以事先回答。