Block control if else

1

The program executes without problems, but always writes the last statement printf("lo siento, no puedo calcularlo");

Is there any way I can not get out of this condition?

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

int main () {
    float i, v, r, a, b=0, c=0;

    printf("calculadora ley de ohm \t\t(Si no tienes un valor pon 0)");
    printf("\n\nIntensidad:\t 0\nVoltaje:\t 0\nResistencia:\t 0\n\n");

    printf("Intensidad? \n");
    scanf("%f", &i);

    printf("Voltaje? \n");
    scanf("%f", &v);

    printf("resistencia? \n");
    scanf("%f", &r);

    printf("\n\nIntensidad:\t %.2f\nVoltaje:\t %.2f\nResistencia:\t %.2f\n", i, v, r);

    a=v/r;
    if (i==0){  
        printf("intensidad: %.2f", a);
        b=i*r;
    }
    else{ 
        if (v==0){ 
            printf("Voltaje: %.2f", b);
            c=v/i;
        }else{ 
            if (r==0){ 
                printf("resistencia: %.2f", c);
            }
            else {
                printf("lo siento, no puedo calcularlo");
            }
        }
    }
    getch();
    return 0;
}
    
asked by Alejandro Caro 15.09.2017 в 01:55
source

3 answers

2

You have a problem in the logic of your program. To begin with, nesting the conditional, when it should not be like that. Unless you do not understand what you are trying to do, the idea is to implement a structure if - else if - else , which would be something like this

if (expresión)
  {
    Bloque de código;
  }
else if(expresión)
  {
    Bloque de código;
  }
else
  {
    Bloque de código;
  }

Even eliminating the nesting the logic is not correct:

  • If I is 0 the code of the if is executed regardless of the value of A and V .

  • If I is not zero but v is 0, what is inside the first else if is executed regardless of the value of R .

  • If I e V are zero but R is not, the code of the second else if is executed.

  • else runs only if all variables are different from 0 .

This is not what you want, in synthesis you need to check which of the three variables is zero (which you want to calculate), if there are more than one that are unknown (or the resistance is negative) you can not calculate anything. This leaves us with three valid possibilities:

  • i = 0 and a ≠ 0 and r > 0
  • r = 0 and i ≠ 0 and r ≠ 0
  • a = 0 and i ≠ 0 and r > 0
  

Note : the intensity and voltage may be negative, but not the resistance.

You need to do these three checks on your construction if- else if . So that the else is executed if none is fulfilled:

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

int main (void)
{
    float i = 0, v = 0, r = 0;

    printf("calculadora ley de ohm \t\t(Si no tienes un valor pon 0)");
    printf("\n\nIntensidad:\t 0\nVoltaje:\t 0\nResistencia:\t 0\n\n");

    printf("Intensidad? \n");
    scanf("%f", &i);

    printf("Voltaje? \n");
    scanf("%f", &v);

    printf("Resistencia? \n");
    scanf("%f", &r);


    if (i==0 && v!=0 && r>0)
    {
        i = v/r;
        printf("\n\nIntensidad:\t %.2f\nVoltaje:\t %.2f\nResistencia:\t %.2f\n", i, v, r);
    }
    else if (v==0 && i!=0 && r>0)
    {
        v = i*r;
        printf("\n\nIntensidad:\t %.2f\nVoltaje:\t %.2f\nResistencia:\t %.2f\n", i, v, r);
    }
    else if (r==0 && v!=0 && i!=0)
    {
        r = v/i;
        printf("\n\nIntensidad:\t %.2f\nVoltaje:\t %.2f\nResistencia:\t %.2f\n", i, v, r);
    }
    else
        printf("\nLo siento, no puedo calcularlo.");


    getch();
    return 0;
}

Exit examples :

law of ohm calculator (If you do not have a value put 0)

Intensidad:      0
Voltaje:         0
Resistencia:     0

Intensidad?
8
Voltaje?
230
Resistencia?
0


Intensidad:      8.00
Voltaje:         230.00
Resistencia:     28.75
calculadora ley de ohm          (Si no tienes un valor pon 0)

Intensidad:      0
Voltaje:         0
Resistencia:     0

Intensidad?
0
Voltaje?
230
Resistencia?
0

Lo siento, no puedo calcularlo.
    
answered by 15.09.2017 / 03:25
source
0

It simply adds in the last else a condition that enables or uninhabits said impression.

    else { if(cond){printf("lo siento, no puedo calcularlo");}
    
answered by 15.09.2017 в 02:24
0

The condition in which you calculate the following value, a , b and c , should not be set to the if in which they are set. Let me explain:

Calculate a , check if V is zero, and calculate b , but ... where do you use it? In the next if , which you will never access.

Using the if - elseif -else blocks, as mentioned above, your code would become somewhat more readable, since when you meet one condition, others are not met (if i is zero, but v is zero too, you have not considered it).

A very useful trick to learn how to correctly use blocks if-elseif-else is to use unit tests, from the most pessimistic to the most optimistic case. You will always eliminate the worst when you start, and you will avoid problems in the long run.

I also strongly recommend that you do not include, from now on, #include <conio.h> , since it is not part of any standard library. You can always use, instead of getch(); , the function getchar(); , included in the library #include <stdio.h> .

    
answered by 15.09.2017 в 09:14