Create a formatted string by use of a string stream. - C++ File Stream

C++ examples for File Stream:stream

Description

Create a formatted string by use of a string stream.

Demo Code

#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
using namespace std;
int main()/*from  ww  w .ja  va2s  .  c  om*/
{
   locale usloc("English_US");
   ostringstream ostr;
   // Set showbase flag so that currency symbol is displayed.
   ostr << showbase;
   // Set the locale of ostr to US English.
   ostr.imbue(usloc);
   // Get a money_put facet for ostr.
   const money_put<char> &us_mon =
   use_facet<money_put<char> >(ostr.getloc());
   // Format a value in US dollars.
   us_mon.put(ostr, false, ostr, ' ', "5498499");
   cout << "Money formatted for US: ";
   cout << ostr.str() << "\n\n";
   // Give a new, empty string to ostr.
   ostr.str(string());
   // Now, construct a table of circular areas.
   ostr << setprecision(4) << showpoint << fixed << left;
   ostr << "Diameter    Area\n";
   cout << "A table of circular areas.\n";
   for(int i=1; i < 10; ++i)
      ostr << left << "   " << setw(6) << i << setw(8) << right << i*3.1416 << endl;
   // Display the formatted string.
   cout << ostr.str();
   return 0;
}

Result


Related Tutorials