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 variableselect
the input buffer is with character ' n'. A possible solution is to use:scanf("%d\n", &select);
.– anonimo
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.
– LUCAS MICHEL DA SILVA
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?– anonimo