String reading and input buffer

Asked

Viewed 339 times

2

I always used the fgets() function to read keyboard strings, because it (at least I thought so) always clears the input buffer. However, I am finding some errors with the execution of the function in the case of the code below. The strings are not being read correctly, it’s like I’m using scanf("%c") and picking up trash from the input buffer.
PS: I know fflush(stdin) cleans the input buffer, but I don’t like using this function because it is not portable.

The struct I’m intending to read in function:

struct Participante
{
    char nome[40];
    char cpf[11];
    char email[20];
    char matricula[15];
    char nascimento[8];
    int ocupacao;
};

The function:

participante* entrada_dados()
{
    participante *part;
    char c;

    part = (participante*) malloc(sizeof(participante));

    printf("Digite o nome do participante \n");
    fgets(part->nome, sizeof(part->nome), stdin);

    printf("Digite o CPF do participante \n");
    fgets(part->cpf, sizeof(part->cpf), stdin);

    printf("Digite o email do participante \n");
    fgets(part->email, sizeof(part->email), stdin);

    printf("Digite a matricula do participante \n");
    fgets(part->matricula, sizeof(part->matricula), stdin);

    printf("Digite a data de nascimento do participante no formato DDMMAAAA \n");
    fgets(part->nascimento, sizeof(part->nascimento), stdin);

    printf("Digite a ocupacao do participante: \n 1- Professor \n 2- Estudante \n");
    scanf("%d", &part->ocupacao);

    return part;

}

Ps2: I am using pointer to structure, because I will return the allocated address and use for the construction of a chained list.

Example of screen with error:

Exemplo de tela com o erro

  • 1

    You have the call to duty entrada_dados() in a certain cycle?

  • That. There is a menu in main and while informing option 1 the input() function is called.

  • You can try to clear the input buffer before the first printf of function entra_dados() with fflush(stdin);.

2 answers

3

Notice that not all input is done with fgets().

The last part, of the participant’s occupation, is made with scanf(). Don’t mix fgets() with scanf().

If necessary uses sscanf().

    char tmp[12];
    printf("Digite a ocupacao do participante: \n 1- Professor \n 2- Estudante \n");
    if (!fgets(tmp, sizeof tmp, stdin)) /* erro */;
    if (sscanf(tmp, "%d", &part->ocupacao) != 1) /* erro */;
  • The error remains occurring, even replacing the scanf with these lines of code. I didn’t understand exactly what the problem is of mixing fgets() and scanf(). Anyway, thank you for answering.

  • As you say yourself: fgets() "clears the input buffer". By mixing this function with scanf() (that does not clear the input buffer) you will need to manage the input buffer manually. Make sure the rest of your code is free of functions that don’t clear the input buffer. Always check the value returned by the error detection functions.

-1

it is very simple I will give a tip that I use a lot`

 struct participante{
char nome[40];
char cpf[11];
char email[20];
char matricula[15];
char nascimento[8];
int ocupacao;}; 
struct participante part;
void p()
{
printf("Digite o nome do participante \n");
scanf("%s",part.nome);

printf("Digite o CPF do participante \n");
scanf("%s",part.cpf);

printf("Digite o email do participante \n");
scanf("%s",part.email);

printf("Digite a matricula do participante \n");
scanf("%s",part.matricula);

printf("Digite a data de nascimento do participante no formato DDMMAAAA \n");
scanf("%s",part.nascimento);

printf("Digite a ocupacao do participante: \n 1- Professor \n 2- Estudante \n");
scanf("%d",&part.ocupacao);

// não precisa dar return pois é só declara a struct como global
}
int main()
{

    p();


} 

ta ai just tested, and this worked correctly I always use this in my code with struct any doubt I’m here

Browser other questions tagged

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