Building Strings with the standard string Class - C++ STL

C++ examples for STL:string

Description

Building Strings with the standard string Class

Demo Code

#include <iostream>
#include <string>   // Include support for string class.
using namespace std;
int main()//from w w w.  j  av a2s .  c  om
{
   string str, name, addr, work;
   // Get three strings from the user.
   cout << "Enter name and press ENTER: ";
   getline(cin, name);
   cout << "Enter address and press ENTER: ";
   getline(cin, addr);
   cout << "Enter workplace and press ENTER: ";
   getline(cin, work);
   // Build the output string, and then print it.
   str = "\nMy name is " + name + ", " + "I live at " + addr + ",\nand I work at " + work + ".\n";
   cout << str << endl;
   return 0;
}

Result


Related Tutorials