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

Temperature Conversion Result

\n\n"; if (get_input()) { double celsius {0.0}; auto it {m.find("celsius")}; if (it == end(m)) { cout << "

Error: No temperature input found.

"; } else { celsius = stod(it->second); double fahrenheit = (celsius * 9.0 / 5.0) + 32.0; cout << "

" << celsius << "° Celsius is equal to " << fahrenheit << "° Fahrenheit.

"; } } else { cout << "

Error: Could not process form input.

"; } cout << "\n" << endl; 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); 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('=')}; if (pos != std::string::npos) { const string key {line.substr(0, pos)}; const string value {line.substr(pos + 1)}; m[key] = value; } } return true; }