Add two conditions C?

3

What I'm trying to do is make a C program, that gives me the final grade of a subject and depending on the number, show me a message on screen.

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

main (){

    float cal1=0, cal2=0, cal3=0, cal4=0;
    float notafinal=0;

    printf("Ingrese la Nota del 1er Corte:  \n\n");
    scanf("%f",&cal1);
    printf ("Ingrese la Nota del 2do Corte:  \n\n");
    scanf("%f",&cal2);
    printf("Ingrese la Nota del 3er Corte:  \n\n");
    scanf("%f",&cal3);
    printf("Ingrese la Nota del 4to Corte:  \n\n");
    scanf("%f",&cal4);

    notafinal=((cal1*0.2)+(cal2*0.2)+(cal3*0.3)+(cal4*0.4));

    if (notafinal > 18){
        printf ("\nYOUR A FUCKING GENIUS\n\n");
        printf ("Su nota es: %f\n\n",notafinal);
    }

    if (notafinal > 12){
        printf("\nAPROBADO\n\n");
        printf("Su nota es: %f\n\n",notafinal);
    }
    else {

        printf("\nREPROBADO\n\n");
        printf("Su nota es: %f\n\n",notafinal);
    }

    return 0;
}

The problem is that it shows both the approved message and the message of a genius , and I want you to just put me first.

Pseudocode: I need to translate something like this but in C.

Si notafinal > 12 escribir APROBADO, SU N...
Si notafinal < 12 escribir DESAPROBADO, SU N...
Si notafinal > 18 escribir GENIO, SU N.. (SIN ESCRIBIR APROBADO, SU NOTA ES...).
    
asked by Romasanta 31.10.2016 в 04:31
source

5 answers

5

Do not confuse:

  • if {...} if{...} Always ask dos times if the variable meets the condition (as is the case) so the first condition is verdadera , that's why the wrong message is given because it verifies that the note is greater than 18 then ask again if it is greater than 12 the two are true if the note is 19 for example print double message
  • if{...} else if{...} will ask dos times if the first condition of if is not met, on the other hand if the first condition is met, it will only enter there and will no longer ask what is in else if()

Modify your if for a else if and it should work very well. Bearing in mind that the validation of the >18 should go in the first if and then the >12 in the else if if the order changes the algorithm fails again. and I would have to use intervals for validation.

if (notafinal > 18){
     printf ("\nYOUR A FUCKING GENIUS\n\n");
     printf ("Su nota es: %f\n\n",notafinal);
}
else if (notafinal > 12 ){
     printf("\nAPROBADO\n\n");
     printf("Su nota es: %f\n\n",notafinal);
}
else {
     printf("\nREPROBADO\n\n");
     printf("Su nota es: %f\n\n",notafinal);
}
    
answered by 31.10.2016 в 07:13
2

I propose this as a solution:

int main()
{
    float cal1=0, cal2=0, cal3=0, cal4=0;
    float notafinal=0;

printf("Ingrese la Nota del 1er Corte:  \n\n");
scanf("%f",&cal1);
printf ("Ingrese la Nota del 2do Corte:  \n\n");
scanf("%f",&cal2);
printf("Ingrese la Nota del 3er Corte:  \n\n");
scanf("%f",&cal3);
printf("Ingrese la Nota del 4to Corte:  \n\n");
scanf("%f",&cal4);

notafinal=((cal1*0.2)+(cal2*0.2)+(cal3*0.3)+(cal4*0.4));

if (notafinal > 18)
{
    printf ("\nYOUR A FUCKING GENIUS\n\n");
    printf ("Su nota es: %f\n\n",notafinal);
}
else if(notafinal > 12)
{
    printf("\nAPROBADO\n\n");
    printf("Su nota es: %f\n\n",notafinal);
}
else
{

    printf("\nREPROBADO\n\n");
    printf("Su nota es: %f\n\n",notafinal);
}
 return 0;
}
    
answered by 31.10.2016 в 06:14
1

Your condition should be this way:

    if (notafinal > 12 && notafinal<=18){
        printf("\nAPROBADO\n\n");
        printf("Su nota es: %f\n\n",notafinal);
    }
    else if (notafinal <=12){
        printf("\nREPROBADO\n\n");
        printf("Su nota es: %f\n\n",notafinal);
}
    
answered by 31.10.2016 в 04:48
0

Simple, you should only add a return in the first if so that if it is a genius, then it ends. The code should look like this:

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

    main (){

    float cal1=0, cal2=0, cal3=0, cal4=0;
    float notafinal=0;

    printf("Ingrese la Nota del 1er Corte:  \n\n");
    scanf("%f",&cal1);
    printf ("Ingrese la Nota del 2do Corte:  \n\n");
    scanf("%f",&cal2);
    printf("Ingrese la Nota del 3er Corte:  \n\n");
    scanf("%f",&cal3);
    printf("Ingrese la Nota del 4to Corte:  \n\n");
    scanf("%f",&cal4);

    notafinal=((cal1*0.2)+(cal2*0.2)+(cal3*0.3)+(cal4*0.4));

    if (notafinal > 18){
        printf ("\nYOUR A FUCKING GENIUS\n\n");
        printf ("Su nota es: %f\n\n",notafinal);
        return 0;
    }

    if (notafinal > 12){
        printf("\nAPROBADO\n\n");
        printf("Su nota es: %f\n\n",notafinal);
    }
    else {

        printf("\nREPROBADO\n\n");
        printf("Su nota es: %f\n\n",notafinal);
    }

    return 0;
}

Probably the previous solutions work, I did not try them, but in this way it is what least modifies your own code. I hope I help you!

    
answered by 31.10.2016 в 21:08
0

Basically it would be like you have been told to change the sequence if... if... by if... else if... else... . Let's see:

if (notafinal > 18){//Entra si es mayor que 18
    printf ("\nYOUR A FUCKING GENIUS\n\n");
    printf ("Su nota es: %f\n\n",notafinal);
}
else if (notafinal > 12){//Aquí te entraría si no ha entrado en el anterior,
    /*Es decir, si notafinal > 12 y <= 18
    *Si sólo pones if, entrará también ya que no importa 
    *la condición anterior, entra al ser mayor que 12
    */
    printf("\nAPROBADO\n\n");
    printf("Su nota es: %f\n\n",notafinal);
}
else {//Y aquí entra si no ha entrado en ninguno de los anteriores
    printf("\nREPROBADO\n\n");
    printf("Su nota es: %f\n\n",notafinal);
}

return 0;
    
answered by 02.11.2016 в 12:07