1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <iostream>
#include <map>
#include <string>
using Point = std::pair<int, int>;
void movement(const std::map<Point, std::string>& places, Point pos);
std::string POS_check(const std::map<Point, std::string>& places, const Point& pos);
int main()
{
std::map<Point, std::string> arr { {{ 4, 5}, "San_Francisco"},
{{123, 5}, "Canberra" } };
Point start {};
movement(arr, start);
}
void movement(const std::map<Point, std::string>& places, Point pos)
{
bool cont = true;
do {
std::cout << "Please enter (N)orth, (S)outh, (E)ast, (W)est, "
"(P)erson, or (Q)uit.\n";
std::cout << pos.first << ", " << pos.second << '\n';
std::cout << "What do you want to do: ";
char get_key;
std::cin >> get_key;
std::cin.ignore();
switch(toupper(get_key)) {
case 'N':
pos.first++;
break;
case 'S':
pos.first--;
break;
case 'E':
pos.second--;
break;
case 'W':
pos.second++;
break;
case 'P':
std::cout << "Operation yet to be implemented.\n";
break;
case 'Q':
cont = false;
break;
default:
std::cout << "You did not enter a valid option.";
break;
}
if(!cont) { break; }
std::cout << POS_check(places, pos) << '\n';
} while (cont);
}
std::string POS_check(const std::map<Point, std::string>& places, const Point& pos)
{
try {
return places.at(pos);
} catch(std::out_of_range& e) {
return "No events here";
}
}
|