sprintf equivalent

A

adamrobillard

Hi,

I went through most of the FAQ but did not find any direct references
formatting outputed data. If I have a application in C like so:

void main(void)
{
int nValue = 100;
double dPi = 3.14159265;
char sName[256] = {"Morpheus"};

printf("Name: %10s, Value: %4d, Pi: %0.2f", sName, nValue, dPi);
getchar();
}

How would I format the output using cout and string variables. Is it
valid to use sprintf to format your output then use cout to display it?
If so, how can you sprintf into a string?

I have read up on .precision etc but after several lines of code I
still do not end up with the results I am looking for. Is there a one
line solution like in C?

Thanks,
 
J

John Harrison

Hi,

I went through most of the FAQ but did not find any direct references
formatting outputed data. If I have a application in C like so:

void main(void)
{
int nValue = 100;
double dPi = 3.14159265;
char sName[256] = {"Morpheus"};

printf("Name: %10s, Value: %4d, Pi: %0.2f", sName, nValue, dPi);
getchar();
}

How would I format the output using cout and string variables. Is it

Something like this

cout << "Name: " << setw(10) << sName << ", Value: " << setw(4) <<
nValue << fixed << setprecision(2) << dPi;

I might have got some of the details wrong. As you can see this stuff is
very tedious.
valid to use sprintf to format your output then use cout to display it?
Yes.

If so, how can you sprintf into a string?

That's not possible. The nearest equivalant is to use a stringstream.

ostringstream buf;
buf << "Name: " << setw(10) << sName << ", Value: " << setw(4) <<
nValue << fixed << setprecision(2) << dPi;
string str = buf.str();
I have read up on .precision etc but after several lines of code I
still do not end up with the results I am looking for. Is there a one
line solution like in C?

No, unless you are prepared to write a lot of code. Fortunately someone
has already done this for you

http://www.boost.org/libs/format/doc/format.html

john
 
J

John Harrison

I guess you missed that

cout.precision(10);
cout << x;

can be written in one line like this

cout << setprecision(10) << x;

You need to #include <iomanip>

John
 
A

adamrobillard

A bit messy compared to C but definitly workable. I will try that, and
look at the boost library.

Thanks,
Adam
 
N

n2xssvv g02gfr12930

A bit messy compared to C but definitly workable. I will try that, and
look at the boost library.

Thanks,
Adam
Only if you only used the standard formatting. May I suggest you create
your own format function objects/classes and overloaded << >> stream
handling functions for your own objects/classes as required. The
resulting code is far more type safe and although it maybe it bit more
work to begin with, the flexible results are worth the effort. Not least
because all streamed I/O is encapsulated for later use.

JFJB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top