int variable changes value when reading a char variable

Asked

Viewed 166 times

2

I’m doing a simple hangman game. However, when I do the reading of the letter (be it with gets or with scanf), the amount of lives drops to 0. I am using Dev-C++, as it is the program 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;
}

1 answer

3


Never, ever, ever dare use the function gets. This hideous function is hated by C programmers for a good reason: It is impossible to use it correctly and any and all ways to use it 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 corruption of the program’s memory.

The function gets was for many years considered obsolete. As of the 2011 C standard, it has been removed to never return (although the GCC still recognizes it). It is too late!

However, your biggest problem is that you don’t have space for the null terminator in the array segredo. Solving this problem and using scanf instead of gets, your program works. Also, your fflush(stdin) and the system("cls") are not portable, which can be solved on the basis of 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;
}

Ah, 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 poisoned by radioactive waste.

  • Use gets may cause skin disease, hair loss, fractures, muscle degeneration, cancer, blindness, fatigue, hepatitis, immunodeficiency, eating disorders, psychotic attacks, hallucinations, psychomotor disorders and various heart, kidney, respiratory problems, circulatory, hepatic, cognitive and behavioral (at least this is what you can acquire by having to debug large programs that use gets).

  • The gets is something dangerous and can addict to the first use. There is no safe level for the use of gets.

  • Live life in its fullness! Say no to gets! Say no to drugs!

Browser other questions tagged

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