Variable int changes value when reading a variable char

2

I'm doing a simple hangman game. However, when I read the letter (either with gets or with scanf ), the number of lives drops to 0. I am using Dev-C ++ because it is the program that the teacher uses in class. >

int main() {

    int i, vida = 3;
    char letra[1], resposta[30] = "azul", segredo[4] = "----";


    do{
        printf("Vidas: %d\t", vida);
        printf("%s\n\n", segredo);

        printf("________________________\n\n");

        printf("Digite uma letra: ");
        fflush(stdin);
        gets(letra);

        for(i = 0; i < strlen(resposta); i++){
            if(letra[0] == resposta[i]){
                segredo[i] = resposta[i];
            }
        }

        system("cls");
    }while(strcmp(resposta, segredo) != 0);

    return 0;
}
    
asked by anonymous 12.11.2017 / 04:37

1 answer

1

Never, ever, ever use the gets function. This horrible function is hated by C programmers for good reason: It is impossible to use it correctly and any and all ways to use gets are wrong. The reason for this is that it starts writing a string in a memory area, but without worrying about where that area ends, which results in program memory corruption.

The gets function has for many years been considered obsolete. From the C standard of 2011, it has been removed to never come back (though the GCC still recognizes it). It's late already!

However, your biggest problem is that you have no space for the null terminator in the segredo array. Solving this problem and using scanf instead of gets , your program works. Also, its fflush(stdin) and system("cls") are not portable, which can be resolved based on in this answer .

See the result:

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

#if defined(__MINGW32__) || defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
#define limpar_input() fflush(stdin)
#define limpar_tela() system("cls")
#else
#define limpar_input() __fpurge(stdin)
#define limpar_tela() system("clear")
#endif

int main() {

    int i, vida = 3;
    char letra;
    char resposta[30] = "azul", segredo[30] = "----";

    do {
        printf("Vidas: %d\t", vida);
        printf("%s\n\n", segredo);

        printf("________________________\n\n");

        printf("Digite uma letra: ");
        scanf("%c", &letra);
        limpar_input();

        for (i = 0; i < strlen(resposta); i++) {
            if (letra == resposta[i]) {
                segredo[i] = resposta[i];
            }
        }

        limpar_tela();
    } while (strcmp(resposta, segredo) != 0);

    return 0;
}

Oh, and Dev-C ++ is a dinosaur too. Use a more modern IDE if possible, such as Code :: Blocks, Eclipse, Netbeans, or Visual Studio.

And remember:

NEVER USE gets

  • Whenever gets is used, a baby whale dies intoxicated by radioactive waste.

  • Using gets can cause skin diseases, hair loss, fractures, muscle degeneration, cancer, blindness, fatigue, hepatitis, immunodeficiency, eating disorders, psychotic attacks, hallucinations, psycho-motor disorders and various problems heart, kidney, respiratory, circulatory, hepatic, cognitive and behavioral (at least this is what you can get when debugging large programs using gets ).

  • gets is something dangerous and can be addictive on first use. There is no safe level for the use of gets .

  • Live life to its fullest! Say no to gets ! Say no to drugs!

12.11.2017 / 05:26