### Distance between two coordinates help

Im beginning an assignment for class and am having trouble with this code. I am trying to find the square root of two points using what I know from class. What is preventing me from finding the distance between two points?

 ``123456789101112131415161718192021222324252627282930313233`` ``````#include #include using namespace std; double calcDistance (int x1, int y1, int x2, int y2); void displayDistance(double distance); int main() { int x1, x2, y1, y2; double distance; cout << "Enter the first x and y coordinates: "; cin >> x1 >> y1; cout << "Enter the second x and y coordinates: "; cin >> x2 >> y2; int distancex = (x2 - x1)^2; int distancey = (y2 - y1)^2; double calcdistance = sqrt(distancex - distancey); return distance; displayDistance(distance); system("pause"); return 0; } void displayDistance(double distance) { cout << "The distance between the two points is: " << distance << endl; } ``````
Your problem is that the carrot caret operator ( ^ ) is the XOR bitwise operator not the power operator. What you want to do is use the pow function

so fix line 18 and 19 in this format ax = `pow( a , x )`

*edit fixed a type I meant caret not carrot

*edit2 You can also simply just add on line 20 this

 ``12`` ``````distancex *= distrancex; //sq distancey *= distancey; //sq ``````
Last edited on
I made the necessary changes but, compiler tells me that it's wrong. Is there a more simpler way to write this?
wait...why do you have line 4 and 22? They are doing nothing.
 I made the necessary changes but, compiler tells me that it's wrong.

The thing about compiler error messages is that they not only tell you exactly what line the error is occurring on, but they also give a very good indication of what the error is.

If you are getting error messages post them here, along with the lines of code that are causing them, and we can explain the messages to you.
it gives me 17 E:\Dev-Cpp\CalculateArea.cpp call of overloaded `pow(int, int)' is ambiguous for some reason... I don't understand why i can't square the numbers directly
do use pow to square 2 numbers. It's overkill.

 ``123`` ``````pow(x,2); // bad (x*x); // good ``````
i dont understand why I cant simply use ^2. Isn't it the same code I had done earlier?

Just to note, I wrote most of this code using what I learned in class recently. So that would show my level of knowledge with syntax right now. Could someone explain why my code won't allow me to show the distance between two points?
Last edited on
 i dont understand why I cant simply use ^2.

^ is not the exponent operator in C++. It's the bitwise XOR operator, so... when you do `x ^ 2` it does something completely different from what you think it does -- is does not square x... instead it toggles bit 1 of x.

See for yourself:

 ``1234`` ``````int foo = 10; int bar = foo^2; cout << bar; // outputs "8" ... not "100" ``````

 Could someone explain why my code won't allow me to show the distance between two points?

1) The formula you're using in your original code is wrong. (even if ^ did what you think it does.. which again it doesn't).

2) You are mixing up two different variables. You have a variable named `distance` that is never initialized because you put the distance into a separate var named `calcdistance`.

3) You exit main (and therefore the entire program) before you print anything. The `return distance;` on line 22... when you `return` from main.. main exits. When main exits.. your program exits. So anything in main after line 22 will not be run.
 ``12345678910111213141516171819202122232425262728`` ``````#include #include using namespace std; void displayDistance(double distance); int main() { int x1, x2, y1, y2; cout << "Enter the first x and y coordinates: "; cin >> x1 >> y1; cout << "Enter the second x and y coordinates: "; cin >> x2 >> y2; int distancex = (x2 - x1) * (x2 - x1); int distancey = (y2 - y1) * (y2 - y1); double distance = sqrt(distancex - distancey); displayDistance(distance); system("pause"); return 0; } void displayDistance(double distance) { cout << "The distance between the two points is: " << distance << endl; }``````

would this be better? It still doesn't do what i want it to do though.
Yes that is much better. Though line 19 is still wrong.

remember the formula is `a2 + b2 = c2`

You are doing `a2 - b2 = c2`
Thank you so much! It finally works! Thanks again Disch!!!