-1

what is wrong it outputs only 0 nothing else

i have 2 kinds b with range 35 and m with range 17 if they can shoot it should output 1 but it doesnt :( also i didnt forgot math.h lib

int main() {

//i got p o k l and kind1 from user

    can_hit(p,o,k,l,kind1);
    printf("%d", can_hit(p, o, k, l, kind1));
    _getch();

}

double distance(int y, int r, int u, int t) {

    return sqrt(((u - y) ^ 2) + ((t - r) ^ 2));


}

int can_hit(int x_0, int y_0, int x_1, int y_1, char kind) {

    int w = 17;
    int e = 35;
    int hit = 0;
    double n = distance(x_0, y_0, x_1, y_1);

    switch (kind) {

    case 'm':

        if (w >= n) {
            hit = 1;
        }
        break;
    case 'b':
        if (e >= n) {
            hit = 1;
        }
        break;
    }
    return hit;
}
  • did you have a prototype for `can_hit` before using it? – phuclv Apr 28 '18 at 11:37
  • 1
    Try some debugging: add various printf statements in your code showing the values of variables along the way. Or try using an actual debugger for the same purpose. It'll probably tell you more precisely where things go wrong. –  Apr 28 '18 at 11:37
  • Without seeing the actual values for p,o,k and l (and kind1), we have no idea: for all we know, the distances are always too large. –  Apr 28 '18 at 11:38
  • 2
    Please upgrade to [mcve], it will make helping you easier. – Yunnosch Apr 28 '18 at 11:40
  • I'm not sure `int` and `double` types concept is OK. Theh are mixed maybe out of controll – Jacek Cz Apr 28 '18 at 11:48
  • @LưuVĩnhPhúc yes i did. i also should have copied that part to here my mistake. – tolga bolat Apr 29 '18 at 10:49

1 Answers1

1

(u - y) ^ 2 does not do what you think it does: ^ is bitwise xor, not power.

Use e.g. (u-y)*(u-y) instead, or pow(u-y, 2). Similar for (t-r) ^ 2 of course.