/* This is a demonstration CGI program that processes data from a simple form. The form has text-input boxes named "x" and "y", and it has a pull-down menu with options "Add", "Subtract", "Multiply", and "Divide". The options are associated with the values "+", "-", "*", and "/". The program does the specified operations on x and y and sends back the result. It does some error checking such as checking that the values entered for x and y are in fact numerical values. */ #include "cgi.h" #include #include int main() { cout << "Content-type: text/html\r\n\r\n"; cout << "\n"; cout << "Result of Your Calculation\n"; cout << "\n"; cout << "

Calculation Result

\n"; cout << "

\n"; if (formItemCount() == 0) { // No CGI data was received. cout << "Sorry, this page is not being accessed correctly.
"; cout << "It's meant to be used with a certain form.

\n"; } else { string xStr = getFormItemValue("x"); // Input from the form. string yStr = getFormItemValue("y"); string op = getFormItemValue("operation"); // "+", "-", "*", or "/" double x,y; // Numerical values of x and y strings. double result; // Result of applying the operation to x and y. if (op != "+" && op != "-" && op != "*" && op != "/") { // Can't be possible if data was received from the right form. cout << "Sorry, this page is not being accessed correctly.
"; cout << "It's meant to be used with a certain form.

\n"; } else if (string2double(xStr,x) && string2double(yStr,y)) { if (op == "+") result = x + y; else if (op == "-") result = x - y; else if (op == "*") result = x * y; else result = x / y; cout << x << " " << op << " " << y << " = " << result; } else { // At least one of xStr or yStr was not a legal number. cout << "Sorry, you must enter numerical values for x and y."; } } cout << "


\n"; cout << "\n"; }