0

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Not enough information I'm afraid. The way you describe it and seeing your code, it should work. Since it does not work you might want to show how these words end up in the arrays. Minor quibble: why twice `scoreCurrent`? – Jongware May 08 '14 at 21:14
  • @Jongware fgets(currentWord[countX], 20, stdin); fscanf(dataMarking, "%[^:]:%[^\n]%*c", scanWord[countX], scanDescription[countX]) Does this help? And the two scoreCurrents is from laziness. – user3258453 May 08 '14 at 21:16
  • 1
    How do you declare `scanWord` and `currentWord`? – Nenad May 08 '14 at 21:16
  • 2
    Yippie. Another occasion of "you forgot to remove the trailing hard return." – Jongware May 08 '14 at 21:16
  • 1
    `fgets()` leaves the newline in the string. Are you sure both strings have newlines? – Barmar May 08 '14 at 21:16
  • I think you nailed it @Barmar :) – pmg May 08 '14 at 21:18
  • @Barmar Ah, that might just be it. How in this case would one add or remove a newline to make it work? – user3258453 May 08 '14 at 21:19
  • @Jongware Then how does one do this in this situation? – user3258453 May 08 '14 at 21:23
  • One searches SO to find this *exact* "problem" (quotes because [it is mentioned in the documentation](http://en.cppreference.com/w/cpp/io/c/fgets)) dozens of times. F.e. http://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input – Jongware May 08 '14 at 21:27
  • @Jongware Thank you, sorry for being so ignorant – user3258453 May 08 '14 at 21:30

2 Answers2

1

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;
    }
}
Jongware
  • 22,200
  • 8
  • 54
  • 100
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks for clearing that up! – user3258453 May 08 '14 at 21:31
  • `n` is always going to be >0, unless an error occured, but it is not a number. fgets returns "`str` on success, NULL on failure" -- where `str` is a pointer to the start of the buffer. – Jongware May 08 '14 at 21:55
  • Thanks, fixed it to use `strlen` to find the last character. – Barmar May 08 '14 at 21:58
  • That's okay, but (sorry to keep on nagging) there is no particular need to create a new `char *`. You can use `strlen(currentWord[countX])` immediately. – Jongware May 08 '14 at 22:09
  • I know. But since I want to check for success, I figured I would create a short variable name instead of indexing. – Barmar May 08 '14 at 22:10
  • Ah -- I see. I'd typically use `if (!fgets(..)) { ..error.. }`. I guess we can leave this as a matter of "personal style" then. – Jongware May 08 '14 at 22:24
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.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131