Fix error game

Asked

Viewed 102 times

0

I think with this game that when I make a mistake at the beginning of the game it tells me that a letter was wrong, but if you hit a letter and then make a mistake it doesn’t count. Help me.

Error: When I start the game asks for a word to be typed. If you miss the letters of that word when the game starts it counts, but if I hit a letter and miss again, the game stops counting the missing errors.

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

int main() { 

char palavra[25],letra[25],lacuna[25]; 
int vida=6,x=0,i,total=0,cont=0; 


printf("                    ******************************");
printf("\n                            JOGO DA FORCA \n");
printf("                    ******************************\n");

printf("\n                             BOM JOGO\n\n");

printf("\nDIGITE A PALAVRA E TECLE ENTER PARA CONTINUAR");
printf("\n\nPALAVRA: ");

gets(palavra); 
fflush(stdin);

system("cls");

for(i=0;i<strlen(palavra);i++) 
{ 
lacuna[i]='X'; 
total++;
cont++;
} 

while(vida>0) 
{ 

printf("\nA PALAVRA COMTEM %i LETRAS\n",total);
printf("\nLETRAS RESTANTES: %i\n",cont);


printf("\n%s\n",lacuna); 
printf("\nENTRE COM UMA LETRA: "); 
gets(letra); 
system("cls");

for(i=0;i<strlen(palavra);i++) 
{ 

if(letra[0]==palavra[i]) 
{ 
lacuna[i]=palavra[i]; 
x++; 
cont--;

} 
} 

if(cont==0){
printf("PARABENS! VOCE VENCEU!");   
printf("\nACERTOU A PALAVRA %s", palavra);
}

if(x==0) 
{ 
vida--; 
printf("\nVOCE PERDEU UMA VIDA!\nVOCE TEM %d VIDA(S) RESTANTES\n\n",vida); 

} 

}

printf("\n\nVC FOI ENFORCADO, Fim de jogo!\n\n\nPALAVRA SECRETA: 
%s",palavra);

printf("\n\n***********************\n\n");
printf("* JOGO DA FORCA *\n\n");
printf(" ___ \n");
printf(" | | \n");
printf(" | O  \n");
printf(" |/|\ \n");
printf(" | |  \n");
printf(" |/ \  \n");
printf(" |______ \n");
printf("\n**********************\n");

getchar(); 
getchar(); 
return 0; 
}
  • How so stop accounting for missing errors?

  • https://answall.com/q/95977/132

  • I suggest you replace gets() for fgets() and look for viable library alternatives conio.h. Doing what I recommended may not change much for this program, but it is always good to implement quality code, even in the simplest projects.

2 answers

1


What is happening is that you asked to show life only when x is 0, and the problem is that when you get the answer right, x goes to 1 and never goes back to 0.

To solve, just turn x to 0 at the end of while again.

    if(x==0) 
    { 
        vida--; 
        printf("\nVOCE PERDEU UMA VIDA!\nVOCE TEM %d VIDA(S) RESTANTES\n\n",vida); 
    } 
    x = 0;
} //Chaves do final do while

-4

#include<stdio.h> 
#include<string.h> 
#include<conio.h> 
#include<stdlib.h> 
#define _CRT_SECURE_NO_WARNINGS
int main() {

    char palavra[25], letra[25], lacuna[25];
    int vida = 6, x = 0, i, total = 0, cont = 0;


    printf("                    ******************************");
    printf("\n                            JOGO DA FORCA \n");
    printf("                    ******************************\n");

    printf("\n                             BOM JOGO\n\n");

    printf("\nDIGITE A PALAVRA E TECLE ENTER PARA CONTINUAR");
    printf("\n\nPALAVRA: ");

    gets(palavra);
    fflush(stdin);

    system("cls");

    for (i = 0; i<strlen(palavra); i++)
    {
        lacuna[i] = 'X';
        total++;
        cont = strlen(palavra);

    }

    while (vida>0)
    {

        printf("\nA PALAVRA COMTEM %i LETRAS\n", total);
        printf("\nLETRAS RESTANTES: %i\n", cont);
        printf("\Vidas Disponiveis: %i\n", vida);

        printf("\n%s\n", lacuna);
        printf("\nENTRE COM UMA LETRA: ");
        gets(letra);
        system("cls");

        for (i = 0; i<strlen(palavra); i++)
        {

            if (letra[0] == palavra[i])
            {
                lacuna[i] = palavra[i];
                x++;
                cont--;
            }

        }

        if (cont == 0){
            printf("PARABENS! VOCE VENCEU!");
            printf("\nACERTOU A PALAVRA %s", palavra);
            main();
            vida = 6;
        }

        if (x == 0)
        {
            vida--;
            printf("\nVOCE PERDEU UMA VIDA!\nVOCE TEM %d VIDA(S) RESTANTES\n\n", vida);

        }

    }

    printf("\n\nVC FOI ENFORCADO, Fim de jogo!\n\n\nPALAVRA SECRETA:%s", palavra);

    printf("\n\n***********************\n\n");
    printf("* JOGO DA FORCA *\n\n");
    printf(" ___ \n");
    printf(" | | \n");
    printf(" | O  \n");
    printf(" |/|\ \n");
    printf(" | |  \n");
    printf(" |/ \  \n");
    printf(" |______ \n");
    printf("\n**********************\n");

    getchar();
    getchar();
    return 0;
}
  • It would be interesting to post a brief explanation as well, I see that your last answers are all just loose code. Collaborate on the quality of the questions, in addition to the code, provide a brief explanation to the author.

  • the explanation is in the code

  • There is no comment in the code, and if there was, this approach is not very interesting, because it makes it difficult to read.

  • Unfortunately the friend’s answer is not working. .

  • I couldn’t compile the code.

  • @Claudio , looks at a quality answer, which puts in the answer code and explanation: https://answall.com/a/17408/64969

Show 1 more comment

Browser other questions tagged

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