0

I can't work out why Strchr() is not working in my function. I need to see if the users guess matches any of the letters in a hidden word. It is a Hangman game.

int guessLetter(char* word, int* guessedLetters)
{
    char guess[20];
    char *s;

    printf("Enter your guess: ");
    scanf("%s", &guess);

    s = strchr (word, guess);
    printf("%s", s);

    if (s != NULL) {
        printf ("Good Guess\n");
    } else {
        printf ("Bad Guess\n");
    }

}

No matter if the guess is right or wrong, my else statement is being activated. My printf shows that s is being given the value of Null no matter if the character is in the word or not.

So I guess my problem is with this part of the code:

s = strchr (word, guess);

I am new to C, so I am sure I am just missing something very basic. I have tried to search the web as much as I can, but I don't really seem to be able to understand what I am doing wrong.

Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108
Xarigizel
  • 59
  • 5
  • 2
    [`const char * strchr ( const char * str, int character )`](http://www.cplusplus.com/reference/cstring/strchr/) takes an int input in second parameter. not a char[20]. also your function is returning an undefined int value atm.. – amdixon Jul 04 '15 at 11:09
  • 1
    Parameters of strchr are a character array and a character ( not a character array). – Sourav Kanta Jul 04 '15 at 11:10
  • 1
    Also, remove the `&` from your `scanf`. Don't add `&` when scanning/printing a `%s`. – Spikatrix Jul 04 '15 at 11:12
  • you may need [strstr](http://www.cplusplus.com/reference/cstring/strstr/) – Eric Tsui Jul 04 '15 at 11:12
  • @amdixon. Thanks, it took me a bit to understand why it would need an int parameter but i understand now. – Xarigizel Jul 04 '15 at 11:28
  • by the way, have you `#include ` in your code? If so, you'd get an error on using **strchr(3)** with incorrect parameters. – Luis Colorado Jul 05 '15 at 17:32

1 Answers1

3

strchr takes an int as 2nd argument but you are passing a char*. You Turn on your compiler warnings.

What you wanted is to loop over the word to see if any of the characters are in guess.

s = 0;
for(size_t i=0; word[i]; i++) {
    s = strchr (guess, word[i]);
    if(s) break; //Found a match
}

This would break on the first match and you can modify it if you want to check for all characters in word.

There's an argument mismatch in scanf call too:

scanf("%s", &guess);

should be

scanf("%s", guess);

scanf expects a char* for format string %s but you are passing char(*)[20] i.e. &guess is of type char (*)[20].

P.P
  • 117,907
  • 20
  • 175
  • 238
  • I think i understand a bit better but i dont see in the loop you gave me where it compares anything to 'guess' Oh nvm, i see you changed the loop to add 'guess'. – Xarigizel Jul 04 '15 at 11:18
  • @Xarigizel Mistake. Edited post now. Also, note that `word` has to be a null terminated string. If it isn't, then you need to pass the number of characters in it as another argument and loop over that number. – P.P Jul 04 '15 at 11:19
  • Thanks for the help, for some reason i find it so much harder to understand C compared to C++/Java. Thanks for all the pointers, will make changes now and see if it all works for me – Xarigizel Jul 04 '15 at 11:21
  • 1
    This looks an awful lot like `strcspn()`. – EOF Jul 04 '15 at 11:25