/* * @topic T00315 Apr 18 -- string stream object * @brief Program demonstrates <tt><sstream></tt> header and <tt>stringstream</tt> class */ #include <iostream> #include <string> #include <sstream> using namespace std; int main() { // Our challenge is to construct an SQL statement // and plug in parameters dynamically: /* SELECT employee.ID, employee.Name FROM employee WHERE employee.phone = VARPHONE and employee.type = VARTYPE and employee.zipcode = VARZIP */ // Construct variables that contain SQL parameters: const char EMPLOYEE_TYPE_INTERN = 'I'; string varphone = "123-456-7890"; char vartype = EMPLOYEE_TYPE_INTERN; int varzip = 12345; // The string that will contain the SQL statement: string select_query; // Memory buffer that we can use as a stream device: stringstream buffer; buffer << "SELECT employee.ID, employee.Name\n" << "FROM employee\n" << "WHERE employee.phone = \"" << varphone << "\"\n" << "and employee.type = '" << vartype << "'\n" << "and employee.zipcode = " << varzip << "\n" ; // Extract data from the buffer and store in the string: select_query = buffer.str(); // Display results: cout << select_query << '\n'; return 0; }