Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to allow the user to calculate the distance between cities on a

ID: 3673176 • Letter: W

Question

Write a program to allow the user to calculate the distance between cities on a map.

Sounds pretty complicated, eh? Don't worry, we'll make a few simplifications here:

The distances are calculated in map units on the 2D map.

Each city is given a set of coordinates relative to some point on the map. We'll call this origin (0,0).

There can only be a finite number of cities in the system at a given time (you decide this maximum).

But in order to make the system useful, we'll have to add a few other features:

Each city has a name in addition to its coordinates (you can assume a maximum length for these names; but they must be able to hold spaces: Los Angeles, New York, etc.).

The user selects what action to take from a menu:

Make sure you allow them to choose their options by both the number and the capitalized letter(s) of the choice.

They cannot calculate a distance until at least two cities have been entered.

When they have exactly two cities, assume they are the ends and print their distance.

When they have more than two cities, show them the list of cities and have them pick the two cities to calculate the distance between. Don't let them choose the same city for both ends of the 'trip'.

If your list is full and they choose to enter another city, have them choose to either NOT enter the new city or to overwrite one of the earlier cities (they choose which one). Print the list of cities for them to choose from. (Remember, you still can't exceed your maximum list size!)

Also, detect whether the user entered a number or a name at prompts involving a list of the cities (including overwrite and distance). If they've entered a number, proceed as normal. If they've entered a name, use a search to decide which city it was. (If the name 'matches' multiple cities, you should print a new — hopefully smaller — list of the matches for them to choose from.)

Explanation / Answer


#include <cstdlib>
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#define FALSE 0
#define TRUE !FALSE
using namespace std;
const char space= ' ';
float pi = M_PI;
class WorldDistances{
       
    public:
        fstream thefile;
      
      
        vector<string> list;//for storing strings of cities and their corresponding coordindates
        vector<float> lats;//for storing latitude coorindates as floats
        vector<float> lons;//for storing longitude coorindates as floats
      
      
        void addCity(string city,float x,float y){
            lats.push_back(x);
            lons.push_back(y);
            ostringstream ss;
            ss <<city<<space<<x<<space<<y<<endl;//contructs string of city and its coordinates to be stored in list vector
            string getcontent=ss.str();
            list.push_back(getcontent);
          

            thefile.open("cities.txt",ios::out);
            for(int i=0;i<list.size();i++){
                thefile<<list[i];
  
            }
          
    thefile.close();
  

      
        }
         void clear(){
      
            list.clear();
            lats.clear();
            lons.clear();
            thefile.open("cities.txt",ios::out);
            thefile.close();

         }

        void display(){
        
            ifstream openfile ("cities.txt");
   
            for(int i=0;i<list.size();i++){
                cout<<i+1<<")"<<space<<list[i];
              
            }
            cout<<endl;
      
        openfile.close();
  
        }
        void replace(int city,string newCity,float lat,float lon){
            if(city>list.size() || city<1){
                cout<<"please enter a valid value for the city you want to replace"<<endl;
            }
            else{
              
            ostringstream ss;
            ss <<newCity<<space<<lat<<space<<lon<<endl;
            string getcontent=ss.str();
            list[city-1]=getcontent;
            lats[city-1]=lat;
            lons[city-1]=lon;
            thefile.open("cities.txt",ios::out);
            for(int i=0;i<list.size();i++){
                thefile<<list[i];
            }
            thefile.close();
            cout<<"cities updated!!!";cout<<endl;
              
          
        }
        }
        float getDistance(int x,int y){
            if(x>list.size() || x<1 || y>list.size() || y<1){
                cout<<"both values entered must be valid"<<endl;
            }
            else{
            //fetch longitude and latitude coordinates from lats and lons vectors
            float lat1=(lats[x-1]);
            float lon1=(lons[x-1]);
            float lat2=(lats[y-1]);
            float lon2=(lons[y-1]);
          
            //convert coordinates to radians:
             lat1 = lat1*pi/180;
             lat2 = lat2*pi/180;
             lon1 = lon1*pi/180;
             lon2 = lon2*pi/180;


             float a = (sin(lat1)) * (sin(lat2));
             float b = (cos(lat1) * cos(lat2));
             float c = cos(lon1-lon2);
             float d = a + (b*c);

             float e = acos(d); // inverse of cosine
             e = e * (180/pi); // converting back to degrees
             double linearDistance = (6371 * pi * e)/180; //calculating linear distance
             cout<<"the distance between the two cities in km is: "<<endl<<linearDistance<<endl;
             return linearDistance;
}
        }
          
                 
   void deleteCity(int i){
            if(i>list.size() || i<1){
                cout<<"please enter a valid value for the city you want to delete"<<endl;
            }
            else{
        list.erase(list.begin()+i-1);
            lats.erase(lats.begin()+i-1);
            lons.erase(lons.begin()+i-1);
        thefile.open("cities.txt",ios::out);
            for(int i=0;i<list.size();i++){
                thefile<<list[i];
            }
            thefile.close();
       }
        }
      
        //stores cites from text file in vector
        void loadCities(){
          
         
            }
       

    };

int main(){
    int i =1;
    WorldDistances cities;
    string city;
    string newCity;
    float lat;
    float lon;
    int condition=TRUE;
  
  
  

       while(condition){
           //menu
    cout<<"press 1 to add a city "
            "press 2 to display cities "
            "press 3 to modify a city "
        "press 4 to delete a city "
        "press 5 to clear the list "
        "press 6 to get the distance between two cities "
            "press 7 to quit ";cin>>i;
         
     
     
         
    switch(i)
    {case 1:
      
        cout<< "enter a city: ";cin>>city;
        cout<< "enter a the latitude coordinate: ";cin>>lat;
        cout<< "enter a the longitude coordinate: ";cin>>lon;
        cout<<" ";
      
        cities.addCity(city,lat,lon);
      

        break;
        case 2:
            cities.display();
          
            break;
            case 3:
                int city;
              
                float lat;
                float lon;
                cities.display();
                cout<<"enter a number for the city you want to replace: ";cin>>city;
                cout<<"enter new city name: ";cin>>newCity;
                cout<< "enter a the new latitude coordinate: ";cin>>lat;
                cout<< "enter a the new longitude coordinate: ";cin>>lon;
                cout<<" ";
              
                cities.replace(city,newCity,lat,lon);
              
              
            break;
        case 4:
            int i;
       cities.display();
       cout<<"choose the number for the city you want to delete: ";cin>>i;
       cout<<endl;
       cities.deleteCity(i);
             
            break;
       case 5:
           cities.clear();
                      
           break;
       case 6:
                    int city1;
                    int city2;
            cities.display();
                    cout<<"enter a number for the first city: ";cin>>city1;
                    cout<<"enter a number for the second city: ";cin>>city2;
                    cout<<endl;
                    cities.getDistance(city1,city2);

           break;
       case 7:
           condition=FALSE;
           break;
    default:
      
        cout<<"you entered an invalid value"<<endl;
    
     
        break;
    }
          
       }    
      


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote