#include #include #include #include #include using namespace std; map m; bool get_input(); int main() { cout << "Content-type: text/html\n\n" << "\n" << "\n" << "Vacation Recommender\n" << "\n" << "

Your Vacation Recommendation

\n"; if (get_input()) { // Retrieve FIRST NAME string firstname = (m.find("firstname") != m.end() ? m["firstname"] : "Traveler"); // Retrieve LAST NAME string lastname = (m.find("lastname") != m.end() ? m["lastname"] : ""); // Retrieve COLOR string color = (m.find("color") != m.end() ? m["color"] : "unknown"); // Retrieve SEASON (HTML uses "Seasons") string season = (m.find("Seasons") != m.end() ? m["Seasons"] : "unknown"); // Retrieve Country //string country = (m.find("country") != m.end() ? m["country"] : ""); // Normalize lowercase for logic for (auto &c : color) c = tolower(c); for (auto &s : season) s = tolower(s); // Determine vacation country using two dimensional array instead of the if else statement for future changes. const map> m { {"summer", { {"blue", "Greece"}, {"yellow", "Spain"}, {"", "Italy"} }}, {"winter", { {"red", "Switzerland"}, {"blue", "Iceland"}, {"", "Montenegro"} }}, {"spring", { {"pink", "Japan"}, {"", "Netherlands"} }}, {"fall", { {"green", "USA (New England)"}, {"", "Germany"} }} }; // const string season {get value from widget}; string country; const auto it {m.find(season)}; if (it == end(m)) { //Season not found. cout << "a surprise destination somewhere beautiful"; } else { // const string color {get value from widget}; const map& colormap {it->second}; auto it2 {colormap.find(color)}; if (it2 == end(colormap)) { //Color not found. Find the default destination for this season. it2 = colormap.find(""); country = it2->second; } else { country = it2->second; //the destination for this season and color. } } // Output results cout << "

Hello, " << firstname << " " << lastname << "!

\n"; cout << "

Your favorite color is " << color << " " << "and your favorite season is " << season << ".

\n"; cout << "

You should visit: " << country << "

\n"; } cout << "\n"; return EXIT_SUCCESS; } // ----------------------------- // Read POST data into map m // ----------------------------- bool get_input() { const char *const p = getenv("CONTENT_LENGTH"); if (!p) { cout << "No CONTENT_LENGTH found.\n"; return false; } int content_length = stoi(p); string s(content_length, '\0'); cin.read(&s[0], content_length); if (!cin) { cout << "Error reading POST data.\n"; return false; } stringstream ss(s); string pair; while (getline(ss, pair, '&')) { size_t pos = pair.find('='); if (pos != string::npos) { string key = pair.substr(0, pos); string value = pair.substr(pos + 1); m[key] = value; } } return true; }