Connecting Tech Pros Worldwide Help | Site Map

printing to a string class variable

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 11:30 PM
Illuzioner
Guest
 
Posts: n/a
Default printing to a string class variable

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




  #2  
Old July 22nd, 2005, 11:30 PM
JH Trauntvein
Guest
 
Posts: n/a
Default 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

  #3  
Old July 22nd, 2005, 11:30 PM
alvaro.begue@gmail.com
Guest
 
Posts: n/a
Default Re: printing to a string class variable

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

  #4  
Old July 22nd, 2005, 11:30 PM
Jon Bell
Guest
 
Posts: n/a
Default 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
  #5  
Old July 22nd, 2005, 11:31 PM
Mike Wahler
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.