Hi, guys, I need some help to figure out how to get my program working.
Program description: The purpose of this program is to all the user to input
names of city and their (x, y) coordinates on a 2D map. Once more than 2
different cities are entered, the user has the option to calculate the distance
between any 2 cities. If the user chooses the same 2 cities, then they are
prompted to change cities. On the other hand, if the user has reached the
maximum city allowed in the list, then they are given the option to delete a
city in order to enter another one. Finally, the user has the option to view
all the cities entered.
Here is my code:
Header file:
#ifndef CLASSES_HEADER_INCLUDED
#define CLASSES_HEADER_INCLUDED
class City
{
private:
string city_name;
Point p;
public:
double distance(City other) const
{ return location.distance(other.location); }
};
class City_List
{
vector<City> L;
Point p;
private:
string input(string prompt);
string name_entry(string& name);
bool valid_entry(string name);
bool get_name();
bool get_x();
bool get_y();
public:
void Display();
void Add_City(City c) {L.push_back(c);};
bool Delete(vector<City>::size_type index);
bool Find(City c, string name);
};
class Point
{
double x, y;
public:
Point(void);
Point(double new_x, double new_y);
Point(const Point & p);
void Output(void) const;
void Input(void);
double distance (const Point & other) const;
double get_x(void) const {return x;)
double get_y(void) const {return y;)
void set_x(double new_x);
void set_y(double new_y);
};
#endif
Implementation file:
#include "cityclasses.h"
#include <cmath>
#include <iostream>
using namespace std;
void Point::Input(void)
{
char dummy;
cin >> dummy >> x >> dummy >> y >> dummy;
return;
}
void Point::Output(void) const
{
cout << '(' << x << ", " << y << ')';
return;
}
double Point::distance(const Point & other) const
{
return sqrt(pow(x-other.x, 2.0) + pow(other.y-y, 2.0));
}
void Point::set_x(double new_x)
{
x = new_x;
return;
}
void Point::set_y(double new_y)
{
y = new_y;
return;
}
Point::Point(const Point & p)
{
x = p.x;
y = p.y;
}
Point::Point(void)
{
x = y = 0.0;
}
Point::Point(double new_x, double new_y)
{
set_x(new_x);
set_y(new_y);
}
bool City_List::valid_entry(string name)
{
bool rv=true;
for (int i = 0; i < L.size(); i++)
if(L[i].city_name == name)
rv = false;
return rv;
}
void City_List::Display()
{
if(L.empty() == 0)
{
cout << "List is empty" << endl;
}
else {
for (short i=o; i < L.size(); i++)
{
cout << L[i].city_name;
cout << "Point:" << L[i].p.x << endl;
cout << "Point:" << L[i].p.y << endl;
cout << endl;
}
}
}
string City_List::input(string prompt)
{
cin.ignore(INT_MAX, '\n');
cout << prompt;
string response;
getline (cin, response);
return response;
}
bool City_List::Delete(int index)
{
bool rv=true;
if(index >= 0 && index < L.size())
{
for(int i = index; i < L.size() - 1; i++)
{
L[i] = L[i+1];
L.pop_back();
}
}
else {
cerr >> "Bad index" << endl;
rv = false;
}
return rv;
}
vector<City>::size_type City_List::Find(string name)
{
vector<City>::size_type i = 0;
while( i < L.size() && L[i].city_name != name)
{
i++;
}
return i;
}
Main file:
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include "cityclasses.h"
using namespace std;
int main()
{
Point p;
City c;
char ltr;
string name_delete;
City_List L;
char CAPS;
do
{
cout << "Please choose from the following options below:" << endl;
cout << "1. Add a City information \n 2. Calculate Distance between cities \n 3. Print all cities \n 4. Delete a city \n 5. Quit \n";
cin >> ltr;
cin.ignore(INT_MAX, '\n');
if(isalpha(ltr))
CAPS = toupper(ltr);
if(ltr == '1' || CAPS == 'A' || CAPS == 'B')
{
L.input_city();
}
else if (ltr == '2' || CAPS == 'C' || CAPS == 'D')
{
L.distance();
}
else if (ltr == '3' || CAPS == 'P')
{
L.Display();
}
else if (ltr == '4')
{
name_Delete = L.input("Which city do you want to delete" << endl);
if(!L.Find(c, name_Delete))
{
cerr << "City not found" << endl;
}
}
//cerr << "The city name was not found" << endl;
else if (ltr == '5')
{
cout << "Goodbye" << endl;
}
}
while (ltr != '5');
return 0;
}
Where I am having trouble is the Class City (public part), how can I use this function to calculate the distance between 2 cities once they are entered.
If someone can check my code and help me, that would be very helpful.
Thanks