#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" << "BMI\n" << "\n" << "\n" << "\n" << "

BMI (Body Mass Index)

\n\n"; if (get_input()) { //Input successfully loaded into the map m. int feet {0}; auto it {m.find("feet")}; if (it == end(m)) { cout << "Could not get feet"; } else { feet = stoi(it->second); //string to integer } double inches {0.0}; it = m.find("inches"); if (it == end(m)) { cout << "Could not get inches"; } else { inches = stod(it->second); //string to double } double pounds {0.0}; it = m.find("pounds"); if (it == end(m)) { cout << "Could not get pounds"; } else { pounds = stod(it->second); } //Compute the BMI. const double total_inches {12.0 * feet + inches}; const double centimeters {2.54 * total_inches}; const double meters {centimeters / 100.0}; const double kilograms {0.45359237 * pounds}; const double bmi {kilograms / (meters * meters)}; cout << "

\n" << "With a height of " << feet << " feet " << inches << " inches, or " << centimeters << " centimeters,\n" << "
\n" << "and a weight of " << pounds << " pounds, or " << kilograms << " kilograms,\n" << "
\n" << "your BMI is " << bmi << ".\n" << "
\n" << "That’s "; //HTML right single quote struct category { string name; double bmi; }; const category a[] { {"obese", 30.0}, //obese if above this number {"overweight", 25.0}, {"healthy", 18.5}, {"underweight", 0.0} }; for (auto cat: a) { if (bmi >= cat.bmi) { cout << cat.name; break; } } const string url {"https://www.nhlbi.nih.gov/calculate-your-bmi"}; cout << ", according to the\n" << "NIH.\n" << "

\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; }