Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "City.h"
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- //Prints the contents on city_list
- void print(vector<City> city_list, size_t init_pos = 0) {
- typedef vector<City>::size_type CityPos;
- for(CityPos count = init_pos; count < city_list.size(); count++) {
- cout << "[" << (count + 1) << "]" ;
- cout << city_list[count].get_name() << " | "
- << "(" << city_list[count].get_location().get_x()
- << ", " << city_list[count].get_location().get_y() << ")\n";
- }
- }
- //Allows user to enter a City into the list
- vector<City> enter_info(vector<City> city_list) {
- string name;
- string input;
- Point location = Point();
- bool okay = false;
- cout << "\nPlease enter the name of the city you're adding:\n";
- cin.ignore(INT_MAX,'\n');
- while(!okay) {
- cin.clear();
- getline(cin, input);
- if(cin.fail()) {
- cerr << "\n\aERROR: Entry not Valid!\n"
- << "Please try again:\n";
- }
- else {
- name = input;
- okay = true;
- }
- }
- cin.clear();
- cout << "\nPlease enter the coordinates of the city "
- << "you're adding in (x,y) format:\n";
- location.Input();
- city_list.push_back(City(location, name));
- return city_list;
- }
- //Allows the user to find the distance between two chosen cities
- void print_distance(vector<City> city_list) {
- int i,j;
- if(city_list.size() != 2) {
- cout << "\nPlease select a city from the list to be your first point:\n";
- print(city_list);
- bool okay = false;
- while(!okay) {
- cin >> i;
- if(i < city_list.size() && i >= 0) {
- okay = true;
- }
- else {
- cerr << "\n\aERROR: Entry not Valid!\n";
- }
- }
- cout << "\nPlease select a city from the list to be your second point:\n";
- okay = false;
- while(!okay) {
- cin >> j;
- if(j <= city_list.size()) {
- okay = true;
- }
- else if(i == j){
- cerr << "\n\aERROR: Choose a city other than "
- << city_list[i-1].get_name() << ".\n";
- }
- else {
- cerr << "\n\aERROR: Entry not Valid!\n";
- }
- }
- }
- else {
- i = 1;
- j = 2;
- }
- cout << "\nThe distance between " << city_list[i-1].get_name() << " and "
- << city_list[j-1].get_name() << " is:\n"
- << city_list[i-1].distance(city_list[j-1]);
- }
- int main() {
- vector<City> city_list;
- bool quit = false;
- cout << "\n\n\t\t\tWelcome to City Select";
- while(!quit){
- cout << "\n\nPlease select one of the following options:"
- << "\n1) Enter city Information"
- << "\n2) calculate Distance between two cities"
- << "\n3) Print all cities"
- << "\n4) Quit\n\n";
- char i;
- bool notValid = false;
- do {
- cin >> i;
- if(i == '1' || i == 'E' || i == 'e'){
- city_list = enter_info(city_list);
- }
- else if(i == '2' || i == 'D' || i == 'd'){
- if(city_list.size() >= 2) {
- print_distance(city_list);
- }
- else {
- cerr << "\n\aERROR: Entry not Valid!\n"
- << "Please have 2+ cities stored before"
- << "calculating a distance.\n";
- }
- }
- else if(i == '3' || i == 'P' || i == 'p'){
- print(city_list);
- }
- else if(i == '4' || i == 'Q' || i == 'q'){
- quit = true;
- }
- else {
- notValid = true;
- }
- }while(notValid);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement