Hangman game in C

Asked

Viewed 106 times

-1

Good evening, fellas, I hope you’re all right.

The problem where I need help is a very simple code (hangman game), however, as I am new to programming and am learning C I am not identifying where exactly is the error. The program runs everything normal, the problem is that after I type the first letter of the word the program tries to read and ends the code.

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

int main ()
{
char palavra[21];
char resposta[21];
char letra[21];
char espaco[21]="*";
char erradas[21];
char tamanho;
int tentativas=5, acertos=0, erros=5;
int cont, corretas;


printf(">>>>>>>>>>><<<<<<<<<<\n");
printf(">>> JOGO DA FORCA <<< \n");
printf(">>>>>>>>>><<<<<<<<<<<\n");

printf("\nDigite a palavra que deseja: ");
gets(palavra);


for (cont =0; cont<strlen(palavra); cont++)
    espaco[cont]='*';
    tamanho=strlen(palavra);

    while (erros > 0)
    {
        corretas =0;
        printf("\n%s\n", espaco);
        printf("\nDigite uma letra: ");
        gets(letra);
        printf("\n\tLetra digitada: %s\n", erros);

        for (cont =0; cont <strlen(palavra); cont++)
        {
            if (letra[0] == palavra[cont])
            {
                espaco[cont] = palavra[cont];
                corretas++;
                acertos++;
            }
        }

        if (corretas ==0);
        {
            tentativas--;
            if (tentativas ==0)
            {
                printf("\nVoce Faleceu\n");
                printf("\nA palavra era %s\n", palavra);
                break;
            }
            else
            {
                printf("\nVoce errou uma letra, ainda tem %d chances", tentativas);
                erradas[erros] = letra[0];
                erros++;
            }



        while (acertos == tamanho)
                {
                    printf("\nAcertou a palavra\n");
                    break;
                }
                if (letra == palavra)
                {
                    printf("\nAcertou uma letra\n");
                }
        }

    }

}

  • I believe that here: if (corretas ==0); don’t have this ; otherwise you will be executing the null command if the condition is true. I did not understand this while (acertos == tamanho) { because if it’s true you don’t modify the variables inside the loop and break down, a if?

2 answers

0

It’s a strong suspicion, so I’ll put it right back.

C character arrays must always be terminated in \0 (for your knowledge this represents the value character 0 in the ASCII table, note that this is different from the character representing the number "0" that you type on the keyboard, which has another value).

Even these arrays should be scaled with an extra space in mind for this character \0.

This is necessary because some functions like the strlen() which you called in the code depend on it, as they traverse the array until they find a \0 and stop.

If they do not find it, they continue to advance beyond the size of the array and invade memory occupied by other data, which can cause error in the program and even terminate it prematurely.

So make sure the gets() adds the \0 at the end of the string that was typed. I believe not. So what must be happening is the chain that was typed is breaking the strlen().

You will have to find an alternative then, add this character in the hand or find another function that automatically adds it in the word typing.

  • According to https://pubs.opengroup.org/onlinepubs/9699919799/functions/gets.html: "The gets() Function Shall read bytes from the standard input stream, stdin, into the array pointed to by s, until a <newline> is read or an end-of-file condition is encountered. Any <newline> Shall be discarded and a null byte Shall be placed immediately after the last byte read into the array.". Its use is not recommended because it can cause an overflow of the allocated area without reporting any problem, it is better to use fgets to limit the characters read but eventually you will have to treat the final ' n'

0

Entering only the merit of the program being aborting its execution, the problem is in the function printf, right after the "fill" of the variable letra:

printf("\n\tLetra digitada: %s\n", erros);

Notice that you are using formatting for values that represent strings and not ints. With that, the function printf is receiving a different type than was reported in its formatting, resulting in undefined behavior.

Just change the formatting to the type int:

printf("\n\tLetra digitada: %i\n", erros);

For its variable was declared to have its type int:

int tentativas=5, acertos=0, erros=5;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.