#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
constdouble Pi = 3.1415926;
constdouble radius = 6371.0; // <=== earth's radius - significant constant
double x1, x2, y1, y2;
char answer;
do
{
cout << "Enter location's (latitude and longitude) in degrees: " << endl; // <==== spelling!
cin >> x2 >> y2;
x1 = 50.4547 * (Pi / 180); // <=== might be worth naming Regina's coordinates ... there again, it's nowhere special
y1 = -104.6067 * (Pi / 180);
x2 = x2 * (Pi / 180);
y2 = y2 * (Pi / 180);
double distance = radius * acos(sin(x1) * sin(x2) + cos(x1) * cos(x2) * cos(y1 - y2));
cout << "The distance from Regina to the city you entered is " << distance << " km" << endl;
cout << "Do you want to find the distance from Regina to another city? (y/n)" << endl; // <=== prompt as to how the user should answer
cin >> answer;
} while (answer == 'y' || answer == 'Y'); // <=== only a 1-character answer
cout << "Goodbye" << endl;
return 0;
}
Consider also:
- making the conversion factor (Pi/180) a named variable: you use it a lot;
- checking your input (e.g. longitude should be -180 to 180 degrees, latitude from -90 to 90 degrees.
Move the declaration of answer to the top of the code (before the do...while loop) - as in my coding example (which you can try with the little gear wheel at the top).