Вопрос

I am comparing two strings each coming from arrays.

while(countX<10){
    if(strcmp(scanWord[countX], currentWord[countX]) == 0)
        {scoreCurrent++;scoreCurrent++;}
    countX++;
}

"scanWord[countX]" and "currentWord[countX]" don't compare; each time coming up that they aren't the same even if they are. It works if I compare things that aren't those and I have printed them to screen to check too. They just don't seem to play well. Thanks in advance.

Это было полезно?

Решение

When you're reading the line, remove the newline:

char *line = fgets(currentWord[countX], 20, stdin);
if (line) {
    int len = strlen(line);
    if (line[len-1] == '\n') {
        line[len-1] = 0;
    }
}

Другие советы

The question as I see it:

"scanWord[countX]" and "currentWord[countX]" don't compare; each time coming up that they aren't the same even if they are.

How do you KNOW the two values are the same? Print them out. Print out the length too. If dealing with Unicode of any sort, print out the individual bytes in hexadecimal, because two Unicode characters that look identical may actually be different.

Because the computer will never say the strings are different if they are really the same. The answer to the question as I read it is that the strings actually are different.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top