In article <20***************************@mb-m10.aol.com>,
Illuzioner <il********@aol.com> wrote:
string mytext;
double nn=445566.332211;
what is the proper equivalent of the following c type expression:
sprintf(mytext,"%8.3f",nn); // this is wrong in c++!!
and then to add to it?
mytext += more formatted text
The key concept is "stringstream".
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string mytext;
ostringstream mytextstream;
double nn = 445566.332211;
// do output to a stringstream just like to cout or to a file
mytextstream << fixed << showpoint;
mytextstream << setw(8) << setprecision(3) << nn;
mytextstream << " is the number.";
// extract the contents of the stringstream to a string
mytext = mytextstream.str();
mytext += " Is this OK?\n";
cout << mytext;
return 0;;
}
--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA