-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
}