#include #include //for class stringstream #include //for EXIT_SUCCESS, EXIT_FAILURE, and the function getenv #include //for class string and the functions stoi, stod #include //for class map using namespace std; map m; //will hold information from the form bool get_input(); //will load the information from the form into m int main() { cout << "Content-type: text/html\n\n" //CGI MIME-type header << "\n" << "\n" << "\n" << "Pizza Value Calculator\n" << "\n" << "\n" << "\n" << "

Pizza Value Calculator

\n\n"; if (get_input()) { //Input successfully loaded into the map m. double diameter1 {0.0}; auto it {m.find("diameter1")}; if (it == end(m)) { cout << "Could not get diameter1"; } else { diameter1 = stod(it->second); } double price1 {0.0}; it = m.find("price1"); if (it == end(m)) { cout << "Could not get price1"; } else { price1 = stod(it->second); } double diameter2 {0.0}; it = m.find("diameter2"); if (it == end(m)) { cout << "Could not get diameter2"; } else { diameter2 = stod(it->second); } double price2 {0.0}; it = m.find("price2"); if (it == end(m)) { cout << "Could not get price2"; } else { price2 = stod(it->second); } const double pi {3.14159265358979}; const double area1 {pi * (diameter1 / 2.0) * (diameter1 / 2.0)}; const double area2 {pi * (diameter2 / 2.0) * (diameter2 / 2.0)}; const double ppi1 {price1 / area1}; const double ppi2 {price2 / area2}; cout << "

\n" << "Pizza 1: " << diameter1 << " inches, $" << price1 << ", area = " << area1 << "sq in" << ", price per sq in = $" << ppi1 << "\n" << "
\n" << "Pizza 2: " << diameter2 << " inches, $" << price2 << ", area = " << area2 << "sq in" << ", price per sq in = $" << ppi2 << "\n" << "

\n"; if (ppi1 < ppi2) { cout << "Pizza 1 is the better deal!\n"; } else if (ppi2 < ppi1) { cout << "Pizza 2 is the better deal!\n"; } else { cout << "Both pizzas are the same value!\n"; } cout << "

\n\n"; } cout << "\n" "\n"; return EXIT_SUCCESS; } bool get_input() { const char *const p {getenv("CONTENT_LENGTH")}; if (p == nullptr) { cout << "Environment has no CONTENT_LENGTH\n"; return false; } const int content_length {stoi(p)}; string s; s.resize(content_length); //Make s big enough to hold all the data. cin.read(&s[0], content_length); //Read the data into s. if (!cin) { cout << "Unable to input " << content_length << " bytes.\n"; return false; } stringstream ss {s}; string line; while (getline(ss, line, '&')) { const size_t pos {line.find('=')}; if (pos != std::string::npos) { //If we found the '=', const string key {line.substr(0, pos)}; const string value {line.substr(pos + 1)}; m[key] = value; //m.operator[](key) = value; } } return true; }