4
I’m trying to make a game of hanging in C to go learning better, even because I need to learn more functions such.
While the game is running, I wanted when the user put a letter, it would appear side by side. And when it was wrong too. Example:
Word I want you to hit: cadeira
On the screen will be _ _ _ _ _ _ _
If the user type c, I want the c replace the first _. 
And if he types W (not in the word), I want it to appear below the _ _ _ _ _ _ a message, like:
Erros:
w f g (que são letras que não tem na palavra)
My code was also giving error in fgets that as I’ve been explained, he puts a \n in front of the string, but I did the Trim in the code to take the \n. The first times I ran with Trim in the code he took the \n and everything was perfect, on each other’s side. But now that I try to run the code it is the same as it was before. So:
_
_
_
_
Can someone tell me why he gets like this?
Follows the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char palavra[20];
char forca[20];
char tentativa;
int chances = 5;
int letras, i, j=0, cont;
int start(void)
{
    printf("\nDigite uma palavra: ");
    fgets(palavra, 20, stdin);
    trimEnd(palavra);
    strcpy(forca, palavra);
    letras = strlen(forca);
    for (i=0; i<letras; i++)
    {
        forca[i]=  '_';
    }
}
int jogo(void)
{
    while(chances > 0)
    {
        __fpurge(stdin);
        printf("\nChances: %d - palavras tem %d letras\n\n", chances, letras);
        for (i=0; i<letras; i++)
        {
            printf("\n%c ", forca[i]);
        }
        printf("\n\nDigite uma letra: ");
        scanf("%c", &tentativa);
        cont = 0;
        for (i=0; i<letras; i++)
        {
            if (palavra[i] == tentativa)
            {
                forca[i] == tentativa;
                cont = cont + 1;
                j++;
            }
        }
        if (cont <= 0)
        {
            chances = chances - 1;
        }
        system("clear");
        if (j == letras)
        {
            break;
        }
    }
}
int resultado(void)
{
    if(chances == 0)
    {
        __fpurge(stdin);
        printf("\nChances: %d - palavra tem %d letras\n\n", chances, letras);
        puts(forca);
        printf("\nVocê perdeu. \nA palavra era: ");
        puts(palavra);
    }
    else
    {
        printf("\nParabens, voce acertou a palavra ", chances, letras);
        puts(palavra);
    }
}
void trimEnd(char *str) { //Tira o \n que o fgets lê junto com a variavel pra ir para a ultima linha
    char *end = str + strlen(str) - 1;
    while (end > str && isspace(*end)) end--;
    end++;
    *end = 0;
}
int main()
{
    start();
    system("clear");
    jogo();
    resultado();
    return(0);
}
NOTE: I just made the program ask for the word (which doesn’t make much sense if I want the user to type) because I’m solving this problem first and when everything is ok, then I’ll look for how to randomly draw any word I have inside an array, maybe...
Thanks in advance.
Beware! You used fflush in an input buffer. That’s undefined behavior.
– Pablo Almeida
@That’s why he’s protected by
#ifdef. In windows it is defined: https://msdn.microsoft.com/en-us/library/9yky46tz.aspx - See the part that says// You must flush the input buffer before using gets.-// fflush on input stream is an extension to the C standard. Really, usefflush(stdin)is a horrible thing, but there’s just no portable way to cleanstdin(and yes, I researched about it). If windows had the__fpurgewould be a good start, but he doesn’t have it.– Victor Stafusa
I didn’t know about this one. It explains a lot. Anyway, I usually use this: http://stackoverflow.com/a/26081123/1796236
– Pablo Almeida
Victor Stafusa, thank you so much. I am beginner level even and understood everything you explained. Sure, I’ll read some more to improve my understanding... and try to do according to your explanations. Thank you very much. ;)
– Marcielli Oliveira
When I start reading an answer and I think, "Wow, this guy tried"; "Wow, this guy knows"; "Wow, I couldn’t write an answer like that"... in the end, it’s always written: "Victor Stafusa"... Wtf!
– Daniel