C code skipping user input

Hello! I'm a newbie and I'm trying to make a program to read a list of information entered into it. Until the first line it works, but from the second register it no longer reads the name, putting the second text "SEXO: [M/F]" in the name line and the final result does not come out as expected.

Follows the Code:

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

int main (void) {
    setlocale(LC_ALL, "Portuguese");

    // <.Listagem de Cadastrados.>

    char nome1[40], nome2[40], nome3[40];
    char sexo1,sexo2, sexo3;
    float nota1,nota2,nota3;

    printf("Cadastrando 1° pessoa\n");
    printf("--------------------------\n");
    fflush(stdin);
    printf("NOME: ");
    fgets(nome1,40,stdin);
    printf("SEXO [M/F]: ");
    sexo1 = getchar();
    printf("NOTA: ");
    scanf("%f", &nota1);
    printf("--------------------------\n");

    printf("Cadastrando 2° pessoa\n");
    printf("--------------------------\n");
    fflush(stdin);
    printf("NOME: ");
    fgets(nome2,40,stdin);
    printf("SEXO [M/F]: ");
    sexo2 = getchar();
    printf("NOTA: ");
    scanf("%f", &nota2);
    printf("--------------------------\n");

    printf("Cadastrando 3° pessoa\n");
    printf("--------------------------\n");
    fflush(stdin);
    printf("NOME: ");
    fgets(nome3,40,stdin);
    printf("SEXO [M/F]: ");
    sexo3 = getchar();
    printf("NOTA: ");
    scanf("%f", &nota3);

    system("clear"); // limpar tela - mesma ideia do cls no windows.

    printf("Listagem Completa\n\n");
    printf("------------------------------\n");
    printf("NOME SEXO NOTA");
    printf("%s %c %.2f\n",nome1,sexo1,nota1);
    printf("%s %c %.2f\n",nome2,sexo2,nota2);
    printf("%s %c %.2f\n",nome3,sexo3,nota3);
    printf("------------------------------\n");
}

Could you help me understand this problem? Could you suggest improvements to the code?

Att.

Author: yohlu, 2020-08-06

1 answers

The problem is with scanf (), it reads a float and leaves an empty line in the buffer, so when you use fgets () it reads that empty line.

Only to clear the buffer you used fflush (stdin), which is not recommended and even avoided, so do not use it.

One solution I found was to add a getchar() after each scanf() to read that empty line and the code no longer ignores the inputs.

Then the code looked like this:

#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
int main (void) {
    setlocale(LC_ALL, "Portuguese");

    // <.Listagem de Cadastrados.>

    char nome1[40], nome2[40], nome3[40];
    char sexo1,sexo2, sexo3;
    float nota1,nota2,nota3;


    printf("Cadastrando 1° pessoa\n");
    printf("--------------------------\n");
    printf("NOME: ");
    fgets(nome1,40,stdin);
    printf("SEXO [M/F]: ");
    sexo1 = getchar();
    printf("NOTA: ");
    scanf("%f", &nota1);
    getchar();

    printf("--------------------------\n");

    printf("Cadastrando 2° pessoa\n");
    printf("--------------------------\n");
    printf("NOME: ");
    fgets(nome2,40,stdin);
    printf("SEXO [M/F]: ");
    sexo2 = getchar();
    printf("NOTA: ");
    scanf("%f", &nota2);
    getchar();

    printf("--------------------------\n");

    printf("Cadastrando 3° pessoa\n");
    printf("--------------------------\n");
    printf("NOME: ");
    fgets(nome3,40,stdin);
    printf("SEXO [M/F]: ");
    sexo3 = getchar();
    printf("NOTA: ");
    scanf("%f", &nota3);
    getchar();

    system("clear"); // limpar tela - mesma ideia do cls no windows.

    printf("Listagem Completa\n\n");
    printf("------------------------------\n");
    printf("NOME SEXO NOTA\n");
    printf("%s %c %.2f\n",nome1,sexo1,nota1);
    printf("%s %c %.2f\n",nome2,sexo2,nota2);
    printf("%s %c %.2f\n",nome3,sexo3,nota3);
    printf("------------------------------\n");
}
 0
Author: Bernardo Sant'Anna Costa, 2020-08-06 06:18:28