문제

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