-3

I want to compare the two strings and show the amount of wins for each player. I do not quite understand how the string.h library works, but in searches I've shown that it should work for this comparison

#include <stdio.h>
#include <string.h>

int main()
{
    printf("Player 1: ");
    scanf("%s", &play1);
    printf("Player 2: ");
    scanf("%s", &play2);            
    printf("Total matches: ");
    scanf("%d", &t_matches);

    for (i = 1; i <= t_matches; i++) {
        printf("Winner match %d: ", i);
        scanf("%s", &win1);
        if (strcmp(win1, play1)) {
            p1++; 
        } else if(strcmp (win1, play2)) {
            p2++; 
        }
    }
    printf("%s win %d matches\n", play1, p1);
    printf("%s win %d matches\n", play2, p2);
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 2
    `win1`, `play1` and `play2` are not declared anywhere. – stark Apr 15 '19 at 03:49
  • See also [How do I compare strings in C?](https://stackoverflow.com/questions/1598425/) and [How to properly compare strings in C?](https://stackoverflow.com/questions/8004237) – Jonathan Leffler Apr 15 '19 at 04:29

1 Answers1

2

The strcmp function returns 0 if the strings are equal. You're checking if they are unequal. You instead want:

if (strcmp(win1, play1) == 0) {
    p1++; 
} else if(strcmp (win1, play2) == 0) {
    p2++;
}
dbush
  • 205,898
  • 23
  • 218
  • 273