#include #include #include //for getenv, EXIT_SUCCESS, EXIT_FAILURE #include //for class string #include //for class map #include //for class numeric_limits using namespace std; map m; //will hold information from form bool get_input(); string display_color(const string& s); int main() { cout << "Content-type: text/html\n\n"; //CGI MIME-type header cout << "\n" "\n" "\n" "BMI\n" "\n" "\n" "\n" "

BMI (Body Mass Index)

\n\n"; if (get_input()) { //Input successfully loaded into the map m. const int feet {stoi(m["feet"])}; const double inches {stod(m["inches"])}; const double pounds {stod(m["pounds"])}; 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 << " ("; struct category { string name; double bmi; }; const category a[] { {"underweight", 18.49999999}, {"normal", 24.9}, {"overweight", 29.9}, {"obese", numeric_limits::max()} }; const size_t n {size(a)}; for (int i {0}; i < n; ++i) { if (bmi <= a[i].bmi) { cout << a[i].name; break; } } cout << ").\n" << "

\n\n"; const string url {"https://www.nhlbi.nih.gov/" "health/educational/lose_wt/BMI/bmi-m.htm"}; cout << "

Categories from the\n" << "NIH

\n\n" << "

\n" << "Underweight = <18.5\n" << "
\n" << "Normal weight = 18.5–24.9\n" << "
\n" << "Overweight = 25–29.9\n" << "
\n" << "Obesity = BMI of 30 or greater\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(content_length, '\0'); cin.read(&s[0], content_length); 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('=')}; const string key {line.substr(0, pos)}; const string value {line.substr(pos + 1)}; m[key] = value; //m.operator[](key) = value; } return true; }