-2

I'm having this code, it's like a calculator of some operators, but it only works until the conditional starts, anyone knows the mistake there? I know it is in spanish, so i'm trying to calculate the distance between two points, but my main question is what happens when i have a conditional if with more than a printf, it just doesn't work that way, can someone explain me? thanks a lot

#include <stdio.h>
#include <math.h>
int main()
{
    float n, PO, x1, x2, y1, y2, i, POO;
    printf("Esta es la calculadora de lugar geometrico, por favor ingresa una cordenada con un espacio entre x y y ");
    scanf("%f %f", &x1, &y1);
    printf("Ahora, presione 1 si el otro numero será una coordenada,2 si es un numero positivo y negativo, y 3 si hablamos de una recta ");
    scanf("%f", n);
    if (n == 1)
    {
        printf("Ingresa la coordenada de la misma forma que la anterior");
        scanf("%f %f", &x2, &y2);
        printf("Ahora vamos a calcular la distancia entre cada uno de los puntos, junto con un punto P(x,y)");
        PO = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
        printf("La distancia entre los puntos indicados es %f", PO);
    }
    if (n == 2)
    {
        printf("Ingresa la coordenada de la misma forma que la anterior");
        scanf("%f %f", &x2, &y2);
        printf("Ahora ingresa el numero con su valor absoluto: ");
        scanf("%f", i);
        printf("Ahora vamos a calcular la distancia entre cada uno de los puntos, junto con un punto P(x,y)");
        PO = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) - i);
        printf("La distancia entre los puntos indicados (usando su valor positivo) es %f", PO);
        POO = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) - i);
        printf("La distancia entre los puntos indicados (usando su valor negativo) es %f", POO);
    }
    if (n == 3)
    {
        printf("Ingresa la coordenada de la misma forma de la anterior");
        scanf("%f", &x2, &y2);
        printf("Ahora vamos a calcular la distancia entre cada uno de los puntos, junto con un punto P(x,y)");
        PO = sqrt((x2 - x1) * (x2 - x1) - abs((y2 - y1) * (y2 - y1)));
        printf("La distancia entre los puntos indicados es %f", PO);
    }
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • 1
    For one thing, you probably meant `scanf("%f", &n);` (must be address of `n`). Same thing later with `i`. – Fred Larson Sep 07 '21 at 19:26
  • thank you, do you know if i take off the syntax, the program can use an if using multiple lines? i know its a simple question, but i alwaysget stuck with any program using that – Juanita Saavedra Sep 07 '21 at 19:32
  • I would like to suggest you use `else` with your `if`s. `if (n == 1) { ... } else if (n == 2) { ... }` There is no reason `n` could not be modified during one of those conditionals so that the following conditionals run, and I'm assuming this is not your intention. – Chris Sep 07 '21 at 19:36
  • Also, i did correct that thing and it still doesn't work, do you know what else can i do? – Juanita Saavedra Sep 07 '21 at 19:37
  • I think you need to be more specific than "doesn't work". What are you using for input? What to do you expect the result to be? What result are you getting? – Fred Larson Sep 07 '21 at 19:46
  • This doesn't make sense either: `scanf("%f", &x2, &y2);` Note there are *two* target arguments provided for *one* actual scan format. – WhozCraig Sep 07 '21 at 20:29
  • 2
    `abs()` is an `int` function. Perhaps you want `PO = sqrt((x2 - x1) * (x2 - x1) - fabs((y2 - y1) * (y2 - y1)));`? Tip: enable all compiler warnings to save time. Faster and more productive than posting on SO. – chux - Reinstate Monica Sep 07 '21 at 20:30

1 Answers1

-4

You can't compare a float value with an integer value. For internal rappresentation It can generate invalid result. For buffer problem can you use fgets and convert string in type?

ChangeJob
  • 41
  • 1
  • 3
  • Well, I'm a new C programmer and i'm only using it for school so i don't get what you say; so, i have to change the line 5 to int n,PO, x1, x2,y1, y2, i, POO; ? – Juanita Saavedra Sep 07 '21 at 19:34
  • 2
    Yes, you can compare float values with integers *if* you know what you are doing. And since `float` can represent small integers exactly, it would work in this case. But I agree that it is unclean code. When working with integers, one should not use floats – HAL9000 Sep 07 '21 at 19:35
  • @JuanitaSaavedra, only `n` should be `int`, the rest can stay `float`, or even better, `double`. – HAL9000 Sep 07 '21 at 19:37
  • similarly, it's often helpful when learning to create a function yourself to do comparisons by subtracting 'em off and checking for a range of accuracy! Do also see [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) for some helpful/dire warnings about floating point math – ti7 Sep 07 '21 at 19:38
  • Yes. Use same type for compare. – ChangeJob Sep 07 '21 at 19:39
  • This answer is a bit too brief IMO. Could you add _why_ this is not a good idea? Any exceptions? What is the "buffer problem" and why does the proposed solution resolve it? Can you give a code example? Are there any references you can cite? – Yun Sep 07 '21 at 23:32