473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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
6 14222
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
> 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

"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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alfred E Neuman | last post by:
I've read that... AClass ac = AClass( "name", 23 ); is less efficient than... AClass ac( "name", 23 ); because the first has to create a temporary object which is then assigned to ac. ...
10
by: ST | last post by:
Hi, I'm new to vb.net programming, and I keep getting this error: Name 'GetQuery' is not declared. I can't figure out why?? It seems like I have the right references/namespaces. This is my code...
12
by: sugaray | last post by:
does the expression int a=printf("%d\n",a); implementation dependent ? I supposed it would produced the result 2, but i got 4206596 (result may vary on your machine), i wonder if the value produced...
14
by: spike | last post by:
Im trying to write a program that should read through a binary file searching for the character sequence "\name\" Then it should read the characters following the "\name\" sequence until a NULL...
2
by: Prabhudhas Peter | last post by:
I need a ComboBox property like "ItemData" in VB.Net as in VB 6.0. The Idea behind this question is, i've created a user control combo box in vb.net with my own set of properties..now i want to...
5
by: Stewart | last post by:
Hi there, I would like to calculate a person's age in years (between 2 dates). I am using the following function for this calculation (source: http://www.mvps.org/access/datetime/date0001.htm)...
51
by: Spidey | last post by:
for(;0;) printf("hello"); even the condition s wrong i get a hello printed on the screen y s this happening
1
by: mark | last post by:
Forgive me if this seems like a stupid question but I need help... I'm trying to do a simple online form that emails me the results from a few fields. Here is the code: <form...
3
by: Ioannis Vranos | last post by:
Is printf("%ls") for printing wchar_t strings defined in C90, or it was added in C95?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.