why this won t work?

Hello. I am new to c++ and i wonder why this doesn t work. All i want is to calculate the distance beetween 2 points on a plane. However, calculating the distance doesn t work. Please tell me why. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  int x1;
printf("Coordinate x of point 1 is:  ");
scanf("%d", &x1);

int y1;
printf("Coordinate y of point 1 is:  ");
scanf("%d", &y1);

printf("point 1 = A(%d, %d) ", x1, y1);

int x2;
printf("coordinate x of point 2 is:  ");
scanf("%d", &x2);

int y2;
printf("coordinate y of point 2 is:  ");
scanf("%d", &y2);

printf("point 2 = B(%d, %d) ", x2, y2);
I don't see you calculating the distance anywhere in the code.
I just took a quick look on my way out. In order to calculate distance you need to use the distance formula and plug in the variables you used for your 2 points.

The distance formula is d=sqrt((x2-x1)^2-(y2-y1)^2)

You will need to include this formula into the code in order to calculate the distance. Hope this helps!
Oh sorry. When i copypasted the code i forgot to copy this also:

double distance = sqrt(((x2 - x1)^2) + ((y2 - y1)^2));
printf("Distance beetween two points is:%g\n", &distance);

I had this in my file too, but it still won t work. Any idea?
^ is not exponentiation, but bitwise xor.
Yes, i tried that now, but it still won t work:

double razdalja = sqrt((pow(x2 - x1,2)) + (pow(y2 - y1,2)));
printf("Distance beetween two points is:%d\n", &razdalja);
%d in a printf format string is used to print integers, not doubles. Try %g instead:
printf("Distance beetween two points is:%g\n", &razdalja);
yes i tried %g before %d - . Doesn t work... when i run the program there are always wrong numbers calculated , or really big ones or sth like that.
Ah. You're passing the address of the double instead of the double. This is why printf() is dangerous. So the correct line is:
printf("Distance beetween two points is:%g\n", razdalja);
Topic archived. No new replies allowed.