Advertisement
Guest User

Modified

a guest
Jul 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "City.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. //Prints the contents on city_list
  9. void print(vector<City> city_list, size_t init_pos = 0) {
  10.    typedef vector<City>::size_type CityPos;
  11.    for(CityPos count = init_pos; count < city_list.size(); count++) {
  12.       cout << "[" << (count + 1) << "]" ;
  13.       cout << city_list[count].get_name() << " | "
  14.            << "(" << city_list[count].get_location().get_x()
  15.            << ", " << city_list[count].get_location().get_y() << ")\n";
  16.    }
  17. }
  18.  
  19. //Allows user to enter a City into the list
  20. vector<City> enter_info(vector<City> city_list) {
  21.    string name;
  22.    string input;
  23.    Point location = Point();
  24.    bool okay = false;
  25.    cout << "\nPlease enter the name of the city you're adding:\n";
  26.    cin.ignore(INT_MAX,'\n');
  27.    while(!okay) {
  28.       cin.clear();
  29.       getline(cin, input);
  30.       if(cin.fail()) {
  31.          cerr << "\n\aERROR: Entry not Valid!\n"
  32.               << "Please try again:\n";
  33.       }
  34.       else {
  35.          name = input;
  36.          okay = true;
  37.       }
  38.    }
  39.    cin.clear();
  40.  
  41.    cout << "\nPlease enter the coordinates of the city "
  42.         << "you're adding in (x,y) format:\n";
  43.    location.Input();
  44.    city_list.push_back(City(location, name));
  45.    return city_list;
  46. }
  47.  
  48. //Allows the user to find the distance between two chosen cities
  49. void print_distance(vector<City> city_list) {
  50.    int i,j;
  51.    if(city_list.size() != 2) {
  52.       cout << "\nPlease select a city from the list to be your first point:\n";
  53.       print(city_list);
  54.       bool okay = false;
  55.       while(!okay) {
  56.          cin >> i;
  57.          if(i < city_list.size() && i >= 0) {
  58.             okay = true;
  59.          }
  60.          else {
  61.             cerr << "\n\aERROR: Entry not Valid!\n";
  62.          }
  63.       }
  64.       cout << "\nPlease select a city from the list to be your second point:\n";
  65.       okay = false;
  66.       while(!okay) {
  67.          cin >> j;
  68.          if(j <= city_list.size()) {
  69.             okay = true;
  70.          }
  71.          else if(i == j){
  72.             cerr << "\n\aERROR: Choose a city other than "
  73.                  << city_list[i-1].get_name() << ".\n";
  74.          }
  75.          else {
  76.             cerr << "\n\aERROR: Entry not Valid!\n";
  77.          }
  78.       }
  79.    }
  80.    else {
  81.       i = 1;
  82.       j = 2;
  83.    }
  84.    cout << "\nThe distance between " << city_list[i-1].get_name() << " and "
  85.         << city_list[j-1].get_name() << " is:\n"
  86.         << city_list[i-1].distance(city_list[j-1]);
  87. }
  88.  
  89. int main() {
  90.    vector<City> city_list;
  91.    bool quit = false;
  92.    cout << "\n\n\t\t\tWelcome to City Select";
  93.    while(!quit){
  94.       cout << "\n\nPlease select one of the following options:"
  95.            << "\n1) Enter city Information"
  96.            << "\n2) calculate Distance between two cities"
  97.            << "\n3) Print all cities"
  98.            << "\n4) Quit\n\n";
  99.       char i;
  100.       bool notValid = false;
  101.       do {
  102.          cin >> i;
  103.          if(i == '1' || i == 'E' || i == 'e'){
  104.             city_list = enter_info(city_list);
  105.          }
  106.          else if(i == '2' || i == 'D' || i == 'd'){
  107.             if(city_list.size() >= 2) {
  108.                print_distance(city_list);
  109.             }
  110.             else {
  111.                cerr << "\n\aERROR: Entry not Valid!\n"
  112.                     << "Please have 2+ cities stored before"
  113.                     << "calculating a distance.\n";
  114.             }
  115.          }
  116.          else if(i == '3' || i == 'P' || i == 'p'){
  117.             print(city_list);
  118.          }
  119.          else if(i == '4' || i == 'Q' || i == 'q'){
  120.             quit = true;
  121.          }
  122.          else {
  123.             notValid = true;
  124.          }
  125.       }while(notValid);
  126.    }
  127.    return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement