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?
#include <iostream>
#include <cmath>
usingnamespace 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;
}
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
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?
^ 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:
1 2 3 4
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.