Input with fgets

Asked

Viewed 100 times

1

Hello, I’m having the following difficulty, the moment I call the function of fgets, regardless of whether on any parole, or in the registration function it does not work. At the moment when compiling shows no error, it simply does not ask for user input. I don’t know if it’s something with the code, if you can help me!

And also how you could store this information in a CSV file instead of matrices.

#include <stdio.h>

#define MAX_LIMIT 50

void cadastro(char str[MAX_LIMIT][50], char id){
    printf("Cadastro de um novo funcionário: \nNome: ");
    fgets(str[id], MAX_LIMIT, stdin);
}

int menu(int select){

    printf("\n Menu do programa: \n");
    printf("[1] Cadastrar novo Funcionário \n");
    printf("[2] Listar Funcionários cadastrados \n");
    printf("[3] Exibir relatório de registro de ponto de um funcionário \n");
    printf("[4] Exibir relatório de pendencias de registro \n");
    printf("[5] Gravar registro do ponto \n");
    printf("[0] Sair do programa \n");
    printf("Qual a opção você deseja: ");
    scanf("%d", &select);
    printf("\n");

    return select;
}

int main(){

    int select = 1;
    char id = 0;
    char str[MAX_LIMIT][50]; 

    while (select != 0){
        select = menu(select);
        if (select == 0){
            printf("Você escolheu sair do programa! \n");
        }
        else if (select == 1){
            cadastro(str, id);
        }
        else if (select == 2){
            printf("Lista de funcionários: ");
        }
        else if (select == 3){
            printf("Opção 3 selecionada \n");
        }
        else if (select == 4){
            printf("Opção 4 selecionada \n");
        }
        else if (select == 5){
            printf("Opção 5 selecionada \n");
        }
        else {
            printf(" Digite um comando válido \n ");
        }
    }
    return 0;
}
  • Although in this specific case it makes no difference (since MAX_LIMIT = 50) it should be: fgets(str[id], 50, stdin);. The problem with your program is that after reading the variable select the input buffer is with character ' n'. A possible solution is to use: scanf("%d\n", &select);.

  • I based your question on n, ok, I partially solved the problem with a setbuf(stdin, NULL), it worked. However it needed to include other inputs after, and these even putting the setbuf before, ends up skipping the fgets.

  • According to https://pubs.opengroup.org/onlinepubs/9699919799/functions/setvbuf.html# the definition of setbuf is assign buffering to a stream. It is not easier just to consume the ' n' or, as said pmg not mix scanf with fgets?

1 answer

2

Your problem is that when the program will run the fgets() the character present in buffer input is an ENTER. In this case, apparently, the fgets() does not work because it reads an empty line, only with the ENTER.

To resolve the situation there are some options:

Clear the buffer input, when necessary, before the fgets()

{ /* limpa stdin */
    int ch;
    do {
        ch = getchar();
        if (ch == '\n') break;
        if (ch == EOF) break; // erro?
    while (1);
}
fgets();

Clear the buffer input after the scanf()

if (scanf("%d", &obj) != 1) /* erro */;
{ /* limpa stdin */
    int ch;
    do {
        ch = getchar();
        if (ch == '\n') break;
        if (ch == EOF) break; // erro?
    while (1);
}

or, always wear fgets() for input, and always maintain the buffer clean input

//if (scanf("%d", &obj) != 1) /* erro */;
fgets(data, sizeof data, stdin);
if (sscanf(data, "%d", &obj) != 1) /* erro */;

Browser other questions tagged

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