// rwstring.cpp -- We can read or write to a string just as we can read or write to a file.
//                 Here we just examine how to use strings with streams.

#include <iostream>
#include <strstream>
#include <string>
using namespace std;

// Read a line (including CR) from cin. Return the resulting string, 
// excluding the CR.
string getline(void)
{
  string line;
  char c;

  while (1) 
    {
      cin.get(c);
      if (c == '\n') return line;
      line += c;
    }
}

void main(void)
{
  while (1)
    {
      cout << "Enter a lastname, a firstname, and an integer: ";
      string aline = getline();
      if (aline == "") break;
      istrstream sin(aline.c_str()); // It associates the C string 
                                // aline.c_str() with the stream sin.
      string lname, fname;
      int age;
      sin >> lname >> fname >> age;  // We read from the C string aline
      char xline[128] = {0};   // We will write to this cstring. Zeroed out.
      ostrstream sout(xline, 128);  // We associate sout to xline.
      sout << fname + " " + lname + " is " << age << " years old";
      cout << xline << endl;
    }
}








