help me finish this homework assignment

Write your question here.
Hello. I am a very beginner to coding.

My code will not compile because it says undeclared identifer at the bottom and underlined the word answer in my while statement.

But I have char answer;
Isn't this the identifier?

Help!
How do make this code work?


#include <iostream>
#include <cmath>


using namespace std;

int main()
{
//
double x1, x2, y1, y2;

do
{
cout << "Enter location's (latitude and lantitude) in degrees: " << endl;
cin >> x2 >> y2;

//Making Pi a constant value to be used as 3.145926
// Convert degrees to radians
const double Pi = 3.1415926;
x1 = 50.4547 * (Pi / 180);
y1 = -104.6067 * (Pi / 180);
x2 = x2 * (Pi / 180);
y2 = y2 * (Pi / 180);

//radius is 6378.10 as outlined in question
//using computation as provided in question
double distance = 6371 * acos(sin(x1) * sin(x2) + cos(x1) * cos(x2) * cos(y1 - y2));

// Results to user after computation
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?" << endl;
char answer;
cin >> answer;


} while ((answer == 'y') || (answer == 'Y') || (answer == 'yes') || (answer == 'Yes'));
cout << "Goodbye" << endl;

return 0;
}
Last edited on
(Please edit posts and put code tags ( <> in the format menu ) around your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
   const double Pi = 3.1415926;
   const double 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.
Last edited on
Thank you for the help!!

I changed it to what you have:


} while (answer == 'y' || answer == 'Y');
cout << "Goodbye" << endl;

return 0;

...it still has the word answer underlined and says "undeclared identifier"

Any other thoughts?
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).
Last edited on
Thank you so so much!!
Topic archived. No new replies allowed.