ClioSport.net

Register a free account today to become a member!
Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

  • When you purchase through links on our site, we may earn an affiliate commission. Read more here.

Anyone good at C++ Programming?



  987 Boxster S
I have a small problem with a distance calculator program I am making. The program runs fine, but I keep getting this strange number, -85899340, as an output. A number 1 to 5 should appear, depending on what number you input at the start of the program. Can anybody have look at what I have done and point me in the right direction? This has been annoying me for hours! Cheers :D

Here is the code:

// Programming 2
// Distance Calculator 2
// By Craig Ashman & Lee Sweetman
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.14159
#define R 6371 // Earth's radius in km
// Declare class to accept and store the coordinates of the transmitter and fault location as radian values
class Distance
{
public:
Distance(
double Lat1, double Lon1, double Lat2, double Lon2, int fault);
double getDistance();
int getFault();
void setFault();
private:
double radLat1, radLon1, radLat2, radLon2; // Coordinates in radians
double Lat, Lon;
double a, c, d;
int f;
void calcDistance();

};
Distance::Distance(
double Lat1, double Lon1, double Lat2, double Lon2, int fault)
{
// Inputted coordinates converted into Radians
radLat1 = Lat1 * PI / 180;
radLon1 = Lon1 * PI / 180;
radLat2 = Lat2 * PI / 180;
radLon2 = Lon2 * PI / 180;
calcDistance();
}
double Distance::getDistance()
{
return d;
}
void Distance::calcDistance()
{
// Haversine Calculation
Lat = radLat2 - radLat1;
Lon = radLon2 - radLon1;
a = pow ( sin(Lat/2), 2 ) + cos(radLat1) * cos(radLat2) * pow ( sin(Lon/2), 2 );

c = 2 * atan2( sqrt(a), sqrt( 1 - a ));

d = R * c;
// Distance in km from transmitter location to problem location
}
int Distance::getFault()
{
return f;
}
void Distance::setFault()
{
int fault;
fault = f;
}
int main()
{
cout <<
" ***** Distance Calculator *****" << endl <<endl;
cout <<
"Enter coordinates of transmitter & problem location to calculate distance" << endl << endl;

char another;
double Lat1, Lon1, Lat2, Lon2;
int fault;

// Input
cout << "Please enter transmitter latitude (signed degrees)" << endl << endl;
cout <<
"Latitude: ";
cin >> Lat1;
cout << endl;
cout <<
"Please enter transmitter longitude (signed degrees)" << endl << endl;
cout <<
"Longitude: ";
cin >> Lon1;
cout << endl << endl;
do{
cout <<
"Please enter problem location latitude (signed degrees)" << endl << endl;
cout <<
"Latitude: ";
cin >> Lat2;
cout << endl;
cout <<
"Please enter problem location longitude (signed degrees)" << endl << endl;
cout <<
"Longitude: ";
cin >> Lon2;
cout << endl;
cout <<
"Enter severity of fault (1 to 5)" << endl << endl;
cout <<
"Severity: ";
cin >> fault;
cout << endl;

// Process
Distance myDistance(Lat1, Lon1, Lat2, Lon2, fault);
// Output

cout << endl <<
"Distance= " << myDistance.getDistance() << "km" << endl << endl;
cout << endl <<
"Fault Severity: " << myDistance.getFault() << endl;
cout << endl <<
"Another set of coorinates? (y/n)";
cin >> another;
cout << endl;
}
while (another == 'y');
char wait;
cin >> wait;
//To hold the accumulator values on the screen
return 0;
}




 


Top