Error in struct C language

Asked

Viewed 56 times

-3

Guys, I’m making a code that the first moment should store the register of patients of a clinic, for this I’m using struct and a variable of this struct to store data for 5 patients. The problem is that, in the execution of the program, when it arrives in the 5th patient, the data display goes beyond what was planned and the code simply closes when I enter the patient’s address. This occurs when compiling by Visual Code, in GDB Online works normally. In neither case is any error indicated and I could not find the reason. Someone gives me a light?

//libraries: #include <stdio. h> #include <locale. h>

//structs

struct PACIENTE {
    int codPaciente;
    char nome[50];
    char endereco[100];
    char telefone[14];
};

//secondary functions

//cadastro de pacientes 
void paciente () {

    //declaração de variáveis
    struct PACIENTE pacientes[4] = { 0 };
    int validacao;

    //processamento de dados
    for (int i = 0 ; i < 5 ; i++) {

        printf("Insira o código do paciente %d: ", i + 1);
        scanf("%i", &pacientes[i].codPaciente);
        setbuf(stdin, NULL);

            if (i > 0) {
                
                do {
                    validacao = 0;

                    for (int j = 0 ; j < i ; j++) {
                        
                        if (pacientes[i].codPaciente == pacientes[j].codPaciente)
                            validacao = 1;
                    }

                    if (validacao == 1) {
                        printf("    Código repetido. Insira outro código para o paciente %d: ", i + 1);
                        scanf("%i", &pacientes[i].codPaciente);
                        setbuf(stdin, NULL);
                    }

                } while (validacao == 1);
            }

        printf("Insira o nome do paciente %d: ", i + 1);
        fgets(pacientes[i].nome, sizeof(pacientes[i].nome), stdin);
        setbuf(stdin, NULL);

        printf("Insira o endereço do paciente %d: ", i + 1);
        fgets(pacientes[i].endereco, sizeof(pacientes[i].endereco), stdin);
        setbuf(stdin, NULL);

        printf("Insira o telefone do paciente %d: ", i + 1);
        fgets(pacientes[i].telefone, sizeof(pacientes[i].telefone), stdin);
        setbuf(stdin, NULL);

        printf("%s\n", pacientes[i].telefone);
    }
}

//core program

int main () {

//definição para idioma
setlocale(LC_ALL,"Portuguese");

//input de dados 
printf("\nBem vindo :) \n\n");

//processamento de dados 
paciente();

printf("\n\n\nFim do programa.\n\n\n");
return 0;  //finalizar o programa

}

1 answer

-2


Dear gufdoor, first I wanted to say good night! Secondly I wanted to emphasize the importance of your question to our community, I am sure that many experienced professionals would make this mistake and I know that your post will be of great use to all types of programmers.

-----solving----

The problem is that struct PACIENTE pacientes[4] = { 0 };, you were unhappy to declare a vector with 4 positions in the intention of using 5. But I must emphasize your code that had several very interesting and different functions. Congratulations! Be very happy with your patients, doctors and consultations, until another day, a strong hug!

Browser other questions tagged

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