I need help with my distance formula. pow is apparently not in cmath or math.h. Since I am a beginner in C++, I need some help with the distance. I did the floaters and everything, well I used a book. Beginning Math and Physics for Game Programmers by Wendy Stahler. I just dont get how to make the distance work. can anyone help me??

Thank You for your help guys. It means a lot to me. =)

``````#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <Windows.h>

using namespace std;

int main()
{
int car1; //Car 1 has 3 coordinates
int car2; //Car 2 has 3 coordinates

// Car 1's ( X, Y, Z ) Coordinates
cout << "What is the X Coordinate of Car 1? ";
cin >> car1;
cout << endl;
cout << "What is the Y Coordinate of Car 1? ";
cin >> car1;
cout << endl;
cout << "What is the Z Coordinate of Car 1? ";
cin >> car1;
cout << endl;
cout << "The coordinates of Car 1 are..." << " ( " << car1 << "," << car1 << "," << car1 << " )" << endl;
cout << endl;

// Car 2's ( X, Y, Z ) Coordinates
cout << "What is the X Coordinate of Car 2? ";
cin >> car2;
cout << endl;
cout << "What is the Y Coordinate of Car 2? ";
cin >> car2;
cout << endl;
cout << "What is the Z Coordinate of Car 2? ";
cin >> car2;
cout << endl;
cout << "The coordinates of Car 2 are..." << " ( " << car2 << "," << car2 << "," << car2 << " )" << endl;
cout << endl;
cout << "The Midpoint of Car's 1 and 2 is.... " << "( " << (car1 + car2) /2 << ", " << (car1 + car2) / 2 << ", " << (car1 + car2) / 2 << " )" << endl;
cout << endl;

float DistanceofTwoCars(float *car1, float *car2);
{
cout << "The Distance between Car 1 and Car 2 is... " << endl;
cout << endl;
cout << "D = " << (float)sqrt(pow(car2 - car1, 2) + pow(car2 - car1, 2) + pow(car2 - car1, 2)) << endl;
};

system("pause");
return 0;
}``````

## All 5 Replies

Looks fine to me. Use floats or doubles instead of integers since ints do not hold decimal places.

There are some serious problems with your code. Here is a fix. I'll let someone explain it, because I don't have enough patience right now.

``````#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>

using namespace std;

void DistanceofTwoCars(float *car1, float *car2){
cout << "The Distance between Car 1 and Car 2 is... " << endl;
cout << endl;
cout << "D = " << (float)sqrt(pow(car2 - car1, 2.0f) + pow(car2 - car1, 2.0f) + pow(car2 - car1, 2.0f)) << endl;
}

int main()
{
float car1; //Car 1 has 3 coordinates
float car2; //Car 2 has 3 coordinates

// Car 1's ( X, Y, Z ) Coordinates
cout << "What is the X Coordinate of Car 1? ";
cin >> car1;
cout << endl;
cout << "What is the Y Coordinate of Car 1? ";
cin >> car1;
cout << endl;
cout << "What is the Z Coordinate of Car 1? ";
cin >> car1;
cout << endl;
cout << "The coordinates of Car 1 are..." << " ( " << car1 << "," << car1 << "," << car1 << " )" << endl;
cout << endl;

// Car 2's ( X, Y, Z ) Coordinates
cout << "What is the X Coordinate of Car 2? ";
cin >> car2;
cout << endl;
cout << "What is the Y Coordinate of Car 2? ";
cin >> car2;
cout << endl;
cout << "What is the Z Coordinate of Car 2? ";
cin >> car2;
cout << endl;
cout << "The coordinates of Car 2 are..." << " ( " << car2 << "," << car2 << "," << car2 << " )" << endl;
cout << endl;
cout << "The Midpoint of Car's 1 and 2 is.... " << "( " << (car1 + car2) /2 << ", " << (car1 + car2) / 2 << ", " << (car1 + car2) / 2 << " )" << endl;
cout << endl;

DistanceofTwoCars(car1,car2);

return 0;
}``````
commented: Thank you so much for your help. I really appreciate you helping me out. +0

Hahahahahaha!!!! You're kidding me. Wow. Thanks!

What firstPerson did was to change 2 to 2.0f which ias a floating point number. This is important because there is no `pow(int, int)` defined in the math library. For a list of the ways you can use `pow()` check this out.

What firstPerson did was to change 2 to 2.0f which ias a floating point number. This is important because there is no `pow(int, int)` defined in the math library. For a list of the ways you can use `pow()` check this out.

Not only that, but he had this inside main

``````float DistanceofTwoCars(float *car1, float *car2);
{
cout << "The Distance between Car 1 and Car 2 is... " << endl;
cout << endl;
cout << "D = " << (float)sqrt(pow(car2 - car1, 2) + pow(car2 - car1, 2) + pow(car2 - car1, 2)) << endl;
};``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.