Difficulty with If/ Else

Asked

Viewed 84 times

-1

Hello! How are you? I’m having a problem when I run my program, it’s a program to calculate four medias and divide by four, and it’s necessary to do this using If and Else, but when I run it, the program reads the first condition, and if the if for Else input the program reads normally, however when the "Student" is approved the program executes the printf of Approved then executes the printf of FAILED, and if it was approved, it was not to execute the failed. Can someone help me?

#include <stdio.h>
#include <conio.h>

int main () {

//VARIAVEIS
float media, nota1, nota2, nota3, nota4, recMedia, receberec;

printf("\n VAMOS CALCULAR SUA NOTA\n");
printf("\nPRESSIONE ENTER...\n");
getch();

//ENTRADA DE DADOS
printf("\n---------------------------\n");
printf("\nDIGITE SUA PRIMEIRA NOTA\n");
scanf("%f", &nota1);
printf("\nDIGITE SUA SEGUNDA NOTA\n");
scanf("%f", &nota2);
printf("\nDIGITE SUA TERCEIRA NOTA\n");
scanf("%f", &nota3);
printf("\nDIGITE SUA QUARTA NOTA\n");
scanf("%f", &nota4);

printf("\n---------------------------\n");
printf("\nPRESSIONE ENTER PARA CALCULAR\n");
getch();

//ENTRADA DE PROCESSAMENTO

printf("\nPRONTO JA VALIDAMOS SUA MEDIA!\nQUE RUFEM OS TAMBORES...\n");
media = (nota1+nota2+nota3+nota4)/4;
printf("\nSUA MEDIA:\t%.2f", media);
printf("\n---------------------------\n");

if (media>=7) {

    printf("\n\tAPROVADO!\n%.2f", media);

    }

    else {

        printf("\n\tREPROVADO!\n");
        printf("\nINSIRA SUA NOTA DE EXAME:");
        scanf("%f",&recMedia);
        printf("\nMEDIA DE EXAME:\t\n%.2f", recMedia);

    }

    receberec = (recMedia+media)/2;
    if (receberec>=7) {

        printf("\n\tAPROVADO EXAME!\n%.2f", receberec);

    } 

    else {

        printf ("\n\tREPROVADO EXAME!\n%.2f", receberec);
    }

printf("\n---------------------------\n");

getch();
printf("\nFIM\n");
system ("PAUSE");
return (0);
}
  • For example if the student gets 10 media, the program shows that he has passed, but also performs the part of failed exam.

  • Matheus, you can edit the question and describe the problem, read the [tour] to know how the site works, if you need help on how to use the tool, visit the [help].

  • Oppa! Yes, thank you very much for your attention... I’ve already edited!

  • 1

    The problem is that you show the exam result anyway, so automatically if the person is approved, they fail the exam (as it will be considered the "zero" of the exam). You need to condition your test to fail only.

  • Thank you very much! I got the problem solved, that was exactly it!

  • If the problem has been solved, upvote the Bacco comment.

Show 1 more comment

1 answer

0

You are not giving an initial value to recMedia. When you have the result "OK" in the first 'if', there is no way to know what is the value of recMedia.

Give an initial value to all variables.

Browser other questions tagged

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