2

I'm making a hangman and everything works fine if you win, but if you lose it doesn't print the word it was.

I was debugging it and I found that in one of the last iterations the first character is changed to '\000' so that is why it doesn't print, but I don't understand why because there is no line where the array word is changed.

#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 selection
    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+1];
    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
    {
        printf("\nBetter luck next time... The word was %s", word);
    }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Ivan.2j
  • 27
  • 5

2 Answers2

3

You're writing to letters out of bounds

char letters[MAX_GUESS+1];
int guesses = 0;
do {
    //...
    letters[guesses+1] = '\0'; // OOPS
    //...
    guesses++;
    //...
} while (... && guesses <= MAX_GUESS);
pmg
  • 106,608
  • 13
  • 126
  • 198
1

Nicely written code, but errors in string management.

Initialize

First time printf("%s", letters); called, letters[] is junk. Initialize it.

// char letters[MAX_GUESS + 1];
char letters[MAX_GUESS + 1] = { 0 };

Too many guess

Off by 1, lower limit.

// ... && guesses <= MAX_GUESS);
... && guesses < MAX_GUESS);

Missing '\0'

strcmp(input, word) uses input as a string, yet it lacks a null character.

Flush

Best to insure output is seen before asking for input.

fflush(stdout); // add
scanf(" %c", &temp_char);

fflush(stdout); // add
scanf(" %s", temp_input);

Unbounded input

scanf(" %s", temp_input); is worse than gets(). Research fgets().

Perhaps more issues.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256