0

I am trying to write the logic for the Hangman game. I want to be able to enter a word and to get a random word from a list.

The part in which that I choose a random word works great without a problem. The problem starts when I enter a word. No matter if I am guessing the correct char it will still count as a strike. What is the explanation for why this is happening?

int main()
{
    int option=5, randomword;
    bool loop = true;
    // here you choose if you want a random word or to enter one by yourself.
    while (loop)
    {
        printf("choose an option:\n0 to enter a word.\n1 for the computer to choose a word for you.\n");
        scanf("%d", &option);
        // start the loop over again if the value that got scan is not 0 or 1;
        if (option == 0 || option == 1)loop = false;
    }
    char EnterdWord[128];
    // if you choose to enter a word it will scan it and enter it to a string. I am using fgets so you can enter space
    if (option == 0)
    {
        printf("enter a word:\n");
        getchar();
        fgets(EnterdWord , 128, stdin);
    }else // if you want a random word, this will make a random number, so later it will choose a random word from a list.
    {
        srand(time(NULL));
        randomword = rand() % 20 ;
    }

    // random words list
    char RanWord[][20] = {"dog", "cat", "sky", "orange", "apple", "movie", "code", "number" ,"ship" ,"google" ,"sushi" , "shower",
                          "super hero", "job interview" ,"computer" ,"video games" ,"pizza" ,"cell phone", "charger", "bottle"};

    char *TempWord = malloc(1024 * sizeof(char));

    // remove the \n from the scan in fgets.
    if (*EnterdWord && EnterdWord[strlen(EnterdWord)-1] == '\n')EnterdWord[strlen(EnterdWord)-1] = 0;
    // give to the string TempWord the word based on the option u choose
    if (option == 0) strcpy(TempWord, EnterdWord);
    if (option == 1) strcpy(TempWord, RanWord[randomword]);

    //this is the string that you are going to scan your right guess.
    char *NiceWord = malloc(1024 * sizeof(char));
    if (NiceWord == NULL) return 0;
    // copy temp word to nice word
    strcpy(NiceWord, TempWord);

    // finding the length of the word for the loop below (I know I can use while != \0)
    int lengh = strlen(TempWord);
    int i=0;
    // this part is for aesthetics. I could have just changed all the characters to space.
    while(i < lengh)
    {
        if (TempWord[i] != ' ')NiceWord[i]='_';
        if (TempWord[i] == ' ')NiceWord[i]=' ';
        i++;
    }

    loop = true;

    char guess;
    // bool verb to check for strikes.
    bool CheckStrike = true;
    int y, strikes=0;
    // print so you can see how many characters the word is.
    printf("\n%s\n", NiceWord);

    while (loop == true)
    {
        y=0;
        //scan your guess.
        printf("enter your guess:\n");
        getchar();
        scanf("%c", &guess);
        // reset the strike checker in case you where right for the last guess
        CheckStrike = true;
        // check if your guess is correct or not.
        while(TempWord[y] != '\0')
        {
            if ( TempWord[y] == guess)
            {
                NiceWord[y] = guess;
                CheckStrike = false;
            }
            y++;
        }
        // if your guess was false, add one strike
        if (CheckStrike == true)strikes++;

        printf("you have %d strikes left\n", 10-strikes);
        printf("%s\n", NiceWord);

        // check if you lost
        if (strikes == 10)
        {
            printf("the word was: %s\nyou lost :( ", TempWord);
            loop = false;
        }
        // check if you won
        if (strcmp (NiceWord, TempWord) == 0)
        {
            printf("\n\n\n\nu won :) with %d strikes left!\n\n\n\n\n", 10-strikes);
            loop = false;
        }
    }

    return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
omercotkd
  • 493
  • 1
  • 6
  • 13
  • See this as a good time to learn how to use a *debugger* to step through your program statement by statement while monitoring variables and their values. – Some programmer dude Jul 07 '21 at 15:44
  • By the way, call `srand` only *once* in your program. Otherwise you might reset the seed to the same value as last time and `rand()` returns the same value as last call. And talking about debugging. try to find a fixed case where your program fails, and skip the random number bit, and instead use a hard-coded value instead. Same for user-input, hard-code it to something known to cause the problem. – Some programmer dude Jul 07 '21 at 15:47
  • You're calling `getchar()` before `scanf()` to read the guess from the user. If the user entered a word, `getchar()` will read the guess. – Barmar Jul 07 '21 at 16:28
  • Get rid of `getchar()` and put a space at the beginning of the scanf format string. – Barmar Jul 07 '21 at 16:28
  • See https://stackoverflow.com/questions/20306659/the-program-doesnt-stop-on-scanfc-ch-line-why – Barmar Jul 07 '21 at 16:28
  • 1
    Don't mix the use of `scanf` and `getchar` and `fgets`. Stick to one input method. – Weather Vane Jul 07 '21 at 17:05
  • 1
    A nice clean way to deal with the newline retained by `fgets` is [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) – Weather Vane Jul 07 '21 at 17:08

1 Answers1

0

Use:

printf("enter your guess:\n");
if (option == 1)
    getchar();
scanf("%c", &guess);
if (option == 0)
    getchar();

This solves the problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
omercotkd
  • 493
  • 1
  • 6
  • 13