我试图得到一个程序,让用户输入一个单词或字符,存储它,然后打印它,直到用户再次键入它,退出程序。我的代码是这样的:

#include <stdio.h>

int main()
{
    char input[40];
    char check[40];
    int i=0;
    printf("Hello!\nPlease enter a word or character:\n");
    gets(input);   /* obsolete function: do not use!! */
    printf("I will now repeat this until you type it back to me.\n");

    while (check != input)
    {
        printf("%s\n", input);
        gets(check);   /* obsolete function: do not use!! */
    }

    printf("Good bye!");
    

    return 0;
}

问题是,我一直得到输入字符串的打印,即使当用户的输入(检查)匹配原始(输入)。我把两者比较错了吗?


当前回答

你不能像这样直接比较数组

array1==array2

你应该一个字符一个字符地比较;为此,你可以使用一个函数并返回一个布尔值(True:1, False:0)。然后您可以在while循环的测试条件中使用它。

试试这个:

#include <stdio.h>
int checker(char input[],char check[]);
int main()
{
    char input[40];
    char check[40];
    int i=0;
    printf("Hello!\nPlease enter a word or character:\n");
    scanf("%s",input);
    printf("I will now repeat this until you type it back to me.\n");
    scanf("%s",check);

    while (!checker(input,check))
    {
        printf("%s\n", input);
        scanf("%s",check);
    }

    printf("Good bye!");

    return 0;
}

int checker(char input[],char check[])
{
    int i,result=1;
    for(i=0; input[i]!='\0' || check[i]!='\0'; i++) {
        if(input[i] != check[i]) {
            result=0;
            break;
        }
    }
    return result;
}

其他回答

你不能使用!=或==比较字符串(有用),你需要使用strcmp:

while (strcmp(check,input) != 0)

这样做的原因是!= and ==将只比较这些字符串的基址。而不是字符串本身的内容。

好的,一些事情,gets是不安全的,应该替换为fgets(input, sizeof(input), stdin)这样你就不会得到缓冲区溢出。

接下来,要比较字符串,必须使用strcmp,其中返回值为0表示两个字符串匹配。使用相等操作符(即。!=)比较两个字符串的地址,而不是它们内部的单个字符。

还要注意,虽然在这个例子中它不会引起问题,但fgets也将换行符'\n'存储在缓冲区中;Gets()不会。如果你比较来自fgets()的用户输入和一个字符串字面值,比如“abc”,它永远不会匹配(除非缓冲区太小,以至于'\n'不能装进去)。

你不能像这样直接比较数组

array1==array2

你应该一个字符一个字符地比较;为此,你可以使用一个函数并返回一个布尔值(True:1, False:0)。然后您可以在while循环的测试条件中使用它。

试试这个:

#include <stdio.h>
int checker(char input[],char check[]);
int main()
{
    char input[40];
    char check[40];
    int i=0;
    printf("Hello!\nPlease enter a word or character:\n");
    scanf("%s",input);
    printf("I will now repeat this until you type it back to me.\n");
    scanf("%s",check);

    while (!checker(input,check))
    {
        printf("%s\n", input);
        scanf("%s",check);
    }

    printf("Good bye!");

    return 0;
}

int checker(char input[],char check[])
{
    int i,result=1;
    for(i=0; input[i]!='\0' || check[i]!='\0'; i++) {
        if(input[i] != check[i]) {
            result=0;
            break;
        }
    }
    return result;
}

使用比较字符串。

它在string.h库中,非常流行。如果字符串相等,STRCMP返回0。下面是strcmp返回内容的更好解释。

基本上,你需要做的是:

while (strcmp(check,input) != 0)

or

while (!strcmp(check,input))

or

while (strcmp(check,input))

您可以查看这个关于strcmp的教程。

您可以:

使用string.h中的strcmp(),这是更简单的版本

或者如果你想自己卷,你可以用这样的东西:

int strcmp(char *s1, char *s2)
{
    int i;
    while(s1[i] != '\0' && s2[i] != '\0')
    {
        if(s1[i] != s2[i])
        {
            return 1;
        }
        i++;
    }
    return 0;
}

我将像这样使用strcmp():

while(strcmp(check, input))
{
    // code here
}