By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,503 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,503 IT Pros & Developers. It's quick & easy.

sprintf equivalent

P: n/a
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,

Nov 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ad***********@gmail.com wrote:
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


Thanks,

Nov 22 '05 #2

P: n/a
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?


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
Nov 22 '05 #3

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

Thanks,
Adam

Nov 22 '05 #4

P: n/a
ad***********@gmail.com wrote:
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
Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.