previous | start | next

Writing a toString() Method: 5

Example. Convert name, ss, id, and amt to a single string.

  string name = "Bob";
  string ss = "123-45-6789";
  int id = 175;
  double amt = 55000.00;

  stringstream ss;

  ss.setf(ios::fixed);
  ss.precision(2);
  ss << "Name: " << name << endl
     << "SSN: " << ss << endl
     << "Id: " << id << endl
     << "Amount: << amt;
  string result = ss.str();
     


previous | start | next