Advertisement
Guest User

Modified

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