0

I'm trying to make a hangman game and everything looks fine until I reach the end where sometimes the game says that the solution is just one character, or if I guess a letter they start changing places to the right. I leave you here with the code and I hope that someone can help me to find my error, thank you!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define VOCABULARY_SIZE 8
#define MAX_STRING 32
#define MAX_GUESS 6

int random_number(int a, int b)
{
    return a + rand() % (b - a + 1);
}

int main()
{
    //random word selsection
    srand((unsigned)time(NULL));
    const char VOCABULARY[VOCABULARY_SIZE][MAX_STRING] = {"vehicle", "building", "shirt", "pencil", "batman", "dromedary", "peach", "hangman"};
    char word[MAX_STRING];
    int i;
    i = random_number(0, VOCABULARY_SIZE - 1);
    strcpy(word, VOCABULARY[i]);

    //user word
    int guesses = 0, length = strlen(word);
    char letters[MAX_GUESS];
    char input[MAX_STRING];
    char temp_char;
    char temp_input[MAX_STRING];
    do
    {
        printf("\nYour entered letters are: ");
        printf("%s", letters);
        printf("\nYour letters found are: ");
        for (int j = 0; j < length; j++)
        {
            if (word[j] == input[j])
            {
                printf("%c", word[j]);
            }
            else
            {
                printf("_");
            }
        }
        printf("\n%d-letter word. %d out of %d failures. Enter a letter: ", length, guesses, MAX_GUESS);
        scanf(" %c", &temp_char);
        letters[guesses] = temp_char;
        letters[guesses+1] = '\0';
        for (int j = 0; j < length; j++)
        {
            if (word[j] == temp_char)
            {
                input[j] = word[j];
            }
        }
        guesses++;
        printf("\nWhat is the word to guess? ");
        scanf(" %s", temp_input);
    } while ((strcmp(input, word) != 0 || strcmp(temp_input, word) != 0) && guesses <= MAX_GUESS);

    if (strcmp(input, word) == 0 || strcmp(temp_input, word) == 0)
    {
        printf("\nCongratulations, the word was %s!", word);
    }
    else if (guesses > MAX_GUESS)
    {
        printf("\nBetter luck next time... The word was %s", word);
    }
}
Ivan.2j
  • 27
  • 5

1 Answers1

1

Your letters array is one entry too small to hold the terminating nul character that you apply in this line:

    letters[guesses+1] = '\0';

You also have an off-by-one error since guesses starts at zero and your loop tests if it's <= MAX_GUESS.

These two errors in combination mean that your letters array is two bytes too small. Likely it spills over into your word array, leaving the last guess and the terminating nul in there when you go to print it.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Thank you! the array size solved a lot of problems, the guesses it is made like that so it goes from 0 to 6 and works like I want but thank you anyways, the only problem I have is that when you fail it foesnt print correctly the word idk why – Ivan.2j Oct 30 '20 at 11:04
  • @Ivan.2j It's because you write two bytes past the end of the `letters` array, likely corrupting the memory where the word is stored. – David Schwartz Oct 30 '20 at 22:30
  • 1
    One fix would be to change `char letters[MAX_GUESS];` to ``char letters[MAX_GUESS]+2];`. One is for the terminating nul is one is because of MAX_GUESS is 6, you allow *7* guesses. – David Schwartz Oct 30 '20 at 22:30