Program does not wait to read the received content

Asked

Viewed 223 times

1

I’m creating a program that reads the student’s name, number of fouls, 3 grades and returns if the student has passed, flunked for foul or flunked for average, knowing that the foul limit is 15, the minimum grade for approval is 7 and that failure for lack overrides failure on average.

But it turns out that after printing on the "type in the number of absences" screen, the program does not expect me to enter a number, but ends the program immediately. What’s the mistake?

    #include <stdio.h>
    int main() {    
char nome;
int f;
float n1,n2,n3;
float mf=(n1+n2+n3)/3;
printf("Digite o nome do aluno: \n");
scanf("%c", &nome);
printf("Digite o numero de faltas do aluno: \n");
scanf("%d", &f);
if (f > 15)
    printf("Aluno reprovado por falta. \n");
else {
    printf("Digite a primeira nota do aluno: \n");
    scanf("%f", &n1);
    printf("Digite a segunda nota do aluno: \n");
    scanf("%f", &n2);
    printf("Digite a terceira nota do aluno: \n");
    scanf("%f", &n3);
    if(mf>=7)
        printf("Aluno aprovado! \n");
    else 
        printf("Aluno reprovado por media. \n");
}
return 0;
}
  • You’re ordering to read only one character, need to ask for one string, and of course, first you need to allocate memory to her.

  • how do I do that?

1 answer

1


The first error described in the question is that you are asking for only one character. You need to ask for one string, with %s in the scanf().

Of course, first you need to allocate memory to her with a array of char.

There is also an error that tries to calculate the average before asking for the notes, which will obviously give wrong result.

Other than that, the code needs to be a little more organized.

#include <stdio.h>
int main() {    
    char nome[30];
    int f = 0;
    printf("Digite o nome do aluno: ");
    scanf("%s", nome);
    printf("\nDigite o numero de faltas do aluno: ");
    scanf("%d", &f);
    if (f > 15) printf("\nAluno reprovado por falta.");
    else {
        float n1, n2, n3;
        printf("\nDigite a primeira nota do aluno: ");
        scanf("%f", &n1);
        printf("\nDigite a segunda nota do aluno: ");
        scanf("%f", &n2);
        printf("\nDigite a terceira nota do aluno: ");
        scanf("%f", &n3);
        float mf = (n1 + n2 + n3) / 3;
        if (mf >= 7) printf("\nAluno aprovado!");
        else printf("\nAluno reprovado por media.");
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • perfect!! thank you very much

Browser other questions tagged

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