0

I have a school assignment to make a hangman game. The game works how I want it to except for one small glitch. If the user entered word is 4 letters or less, the hidden word is displayed with an extra "_\377" at the end. When the user entered word is 5 letters or more, then there is no glitch. I am hoping that someone would be kind enough to help me trouble shoot the problem. Thanks in advance!

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


int letterfinder(char string[], char a, int vari)
{
int length = strlen(string);
int i = vari;
int val = 0;
while( i <= length && val != 1)
{
    if( string[i] == a)
    {
        val = 1;
    }
    i++;
}
if( val == 0)
{
    return 100;
}
else
{
    return i;
}
}



int main()
{
char inWord[] = "1111111111111111111111111111";
char outWord2[] = "1111111111111111111111111111";
char guess;
int gameover = 0;
int trys = 10;
int vari = 0;
printf("Please enter a word: ");
gets(inWord);
printf("%s\n", inWord);
printf("                            \n");
printf("                            \n");
printf("                            \n");
printf("                            \n");
printf("                            \n");
printf("                            \n");
int i2 = 0;
int j2 = 0;
int i3 = 0;
i2 = strcspn(inWord, outWord2);
char outWord[80];
while(i3 < i2)
{
   outWord[i3] = '1';
    i3++;

}

while(j2 < i2)
{
    outWord[j2] = '-';
    j2++;
}
puts(outWord);

while(gameover != 1 )
{
    printf("What is your guess: ");
    scanf("%s", &guess);
    vari = 0;
    if(letterfinder(inWord, guess, vari) == 100)
    {
        printf("Wrong!");
        trys--;
        printf("You have %d attempts left\n", trys);
        if(trys == 0)
        {
            gameover = 1;
            printf("You ran out of attempts. Game over\n");
        }
    }
    else
    {
        outWord[(letterfinder(inWord, guess, vari) - 1)] = guess;
        vari = (letterfinder(inWord, guess, vari));
        while(letterfinder(inWord, guess, vari) != 100)
        {
            outWord[(letterfinder(inWord, guess, vari) - 1)] = guess;
            vari = letterfinder(inWord, guess, vari);
        }
        puts(outWord);

    }
    int value = 0;
    i3 = 0;
    while( i3 <= i2)
       {
           if( outWord[i3] == '-')
           {
               value = 1;
           }
           i3++;
       }
    if(value != 1)
    {
        printf("Congratulations, you have guessed the word!\n");
        gameover = 1;
    }

 }
  return 0;
}
Tim Eilers
  • 1
  • 1
  • 2
  • 5
    Your code has Undefined Behaviour. In the cases it "works" it is only by chance/luck. `char guess; scanf("%s", &guess);` That causes memory corruption as you are writing a string to a variable that can only hold a single char. Even a single letter guess will require two characters to store as all C strings are NUL terminated. – kaylum Dec 13 '16 at 05:30
  • 1
    Please note that [`gets()` is too dangerous to be called — ever!](http://stackoverflow.com/questions/1694036/) – Jonathan Leffler Dec 13 '16 at 05:59
  • Adding this as a further explanation on gets() https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – Gottaquest Oct 14 '18 at 16:23

1 Answers1

0

Your code has Undefined Behaviour. In the cases it "works" it is only by chance/luck. char guess; scanf("%s", &guess); That causes memory corruption as you are writing a string to a variable that can only hold a single char. Even a single letter guess will require two characters to store as all C strings are NUL terminated. – kaylum

Armali
  • 18,255
  • 14
  • 57
  • 171