Connecting Tech Pros Worldwide Forums | Help | Site Map

printing to a string class variable

Illuzioner
Guest
 
Posts: n/a
#1: Jul 23 '05
hey all,

can anyone enlighten me on the proper c++ way to print formatted text into a
string?

e.g.

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

any ideas?

thnx a bunch:)

lou




JH Trauntvein
Guest
 
Posts: n/a
#2: Jul 23 '05

re: printing to a string class variable


The most appropriate way to format into a string value is to use
std::ostringstream declared in <sstream>.

Regards,

Jon Trauntvein

alvaro.begue@gmail.com
Guest
 
Posts: n/a
#3: Jul 23 '05

re: printing to a string class variable


Try this out:
http://www.boost.org/libs/format/

Jon Bell
Guest
 
Posts: n/a
#4: Jul 23 '05

re: printing to a string class variable


In article <20050108052355.07117.00002700@mb-m10.aol.com>,
Illuzioner <illuzioner@aol.com> wrote:[color=blue]
>
>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[/color]

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 <jtbellm4h@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Mike Wahler
Guest
 
Posts: n/a
#5: Jul 23 '05

re: printing to a string class variable



"Illuzioner" <illuzioner@aol.com> wrote in message
news:20050108052355.07117.00002700@mb-m10.aol.com...[color=blue]
> hey all,
>
> can anyone enlighten me on the proper c++ way to print formatted text into[/color]
a[color=blue]
> string?[/color]

There are a couple of ways: Using a 'std::ostringstream'
object, or use 'sprintf()'.
[color=blue]
>
> e.g.
>
> 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++!![/color]

It's wrong because you gave the wrong type argument to
'sprintf()'. So it's wrong in C as well.

#include <ios>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
double nn=445566.332211;
std::ostringstream oss;
oss << std::fixed << std::setprecision(6) << nn;
std::string mytext(oss.str());
std::cout << mytext << '\n';
return 0;
}


[color=blue]
>
> and then to add to it?
>
> mytext += more formatted text[/color]

oss.str("");
oss << " more text " << 42;
mytext += oss.str();

[color=blue]
>
> any ideas?[/color]

Yes, get this book:
www.josuttis.com/libbook

-Mike


Closed Thread