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

Can cout do like printf("Name: %s, Age: %d\n",name,age)?

P: n/a
Dear all,

To do below is too long.
cout << "Name: " << name << ", Age: " << age << endl;
Can cout use "%" to implement?
Thanks.

cylin.

Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Tue, 19 Aug 2003 18:54:29 +0800, cylin <cy***@avant.com.tw> wrote:
Dear all,

To do below is too long.
cout << "Name: " << name << ", Age: " << age << endl;
If 20 characters of typing is enough to make you want to avoid
type safety, then I don't C++ is the language for you. :)
Can cout use "%" to implement?


No.

--
Sam Holden
Jul 19 '05 #2

P: n/a
> To do below is too long.
cout << "Name: " << name << ", Age: " << age << endl;
Why it is too long?
If you would remove the spaces around the '<<' operators (just like the
printf), the cout version is only 4 character longer. That seems to be a
small price to pay for the type safety you get with cout.
Can cout use "%" to implement?


No.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Jul 19 '05 #3

P: n/a

"cylin" <cy***@avant.com.tw> wrote in message
news:bh************@ID-154203.news.uni-berlin.de...
Dear all,

To do below is too long.
cout << "Name: " << name << ", Age: " << age << endl;
Can cout use "%" to implement?
Thanks.

cylin.


No, but boost do a format library that adds this to C++

http://www.boost.org/libs/format/index.htm

john
Jul 19 '05 #4

P: n/a
"cylin" <cy***@avant.com.tw> wrote in message
news:bh************@ID-154203.news.uni-berlin.de...
Dear all,

To do below is too long.
cout << "Name: " << name << ", Age: " << age << endl;
Can cout use "%" to implement?
Thanks.


Your two examples are not equivalent. In the version using cout you flush
the output stream, whereas in the printf version you don't.
Jul 19 '05 #5

P: n/a
In article <bh************@ID-154203.news.uni-berlin.de>,
"cylin" <cy***@avant.com.tw> wrote:
Dear all,

To do below is too long.
cout << "Name: " << name << ", Age: " << age << endl;
Can cout use "%" to implement?
Thanks.

cylin.


Check out boost.format (http://www.boost.org/libs/format/index.htm)
Jul 19 '05 #6

P: n/a
cylin <cy***@avant.com.tw> wrote in message
news:bh************@ID-154203.news.uni-berlin.de...
Dear all,

To do below is too long.
cout << "Name: " << name << ", Age: " << age << endl;
Removing unnecessary whitepace and using
same newline expression in order to form
a valid comparision:

printf("Name: %s, Age: %d\n",name,age);

cout<<"Name: "<<name<<", Age: "<<age<<'\n';

Is an extra four characters really too high
a price to pay for type safety?
Can cout use "%" to implement?


I suppose one could write some code to simulate
it, but what's the point?

Also note that 'printf()' is just as valid
(but less safe) a function in C++ as are the
stream inserters.

If you insist upon using '%' specifiers, why not just
use 'printf()'?

-Mike

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.