Program does not wait to read content received

1

I am creating a program that reads the student's name, the number of absences, 3 grades and returns if the student has passed, failed for failure or failed by average, knowing that the limit of absences is 15, the minimum grade for approval is 7 and that failure for failure overrides disapproval by average.

But it happens that after printing on the screen "enter the number of faults", the program does not expect me to enter a number, but soon ends the program. What is wrong?

    #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;
}
    
asked by anonymous 25.02.2017 / 16:10

1 answer

1

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

Of course you need to allocate memory for it with a array of char .

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

Otherwise, the code needs to be a bit 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.");
        }
    }
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

    
25.02.2017 / 16:53