Error in my fgets can’t add data to it

Asked

Viewed 26 times

-1

I’m with a struct to receive data in char and float, but when will I enter data in the first char with fgets he won’t let me type and already jumps to my second fgets

int ex22()
{
     struct cadastro
     {
         char nome[50];
         char disciplina[30];
         float nota_1;
         float nota_2;
     };
     struct cadastro aluno;

//Nesse primeiro fgets eu não consigo inserir o nome do aluno, ele já pula para disciplina

     printf("\nNome do aluno: ");
     fflush(stdin);
     fgets(aluno.nome,50,stdin);
     
     printf("\nDisciplina: ");
     fflush(stdin);
     fgets(aluno.disciplina, 30, stdin);

     printf("\ndigite nota1: ");
     scanf("%f", &aluno.nota_1);

     printf("\nDigite nota2: ");
     scanf("%f", &aluno.nota_2);
    
     printf("\n----------------Aluno cadastrado---------------\n\n\n");

     printf("\nNome: %s\n", aluno.nome);
     printf("\ndisciplina: %s\n", aluno.disciplina);
     printf("\nnota_1: %.1f\n", aluno.nota_1);
     printf("\nnota_2: %.1f\n", aluno.nota_2);

     system("pause");
     return (0);
 }
  • 1

    Are you sure you are c#?

1 answer

0

I ran that same code here, and it worked, the only difference, was that I took out the system("pause"). Check the header files, if the main ta is well formatted. It can also be the compiler, I used gcc here. Anything comment on this first fflush and/or this second as well.

Browser other questions tagged

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