473,775 Members | 2,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14252
On Tue, 19 Aug 2003 18:54:29 +0800, cylin <cy***@avant.co m.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.merke rk(at)dse.nl


Jul 19 '05 #3

"cylin" <cy***@avant.co m.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.co m.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.co m.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.co m.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
2089
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. However, I can't seem to prove this true. I've created a class containing the following constructors and overloaded equal operator.
10
6702
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 below. Also, can someone explain to me what the ContentHandlerImpl.vb file actually does? It's in this project that I'm working with...but I'm not sure what the file actually does. Thanks!! Imports System.Xml Imports Lucene.Net.Documents
12
3811
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 was a memory address and anything special with this expression ?
14
3775
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 character is encountered. But when my program runs it gets a SIGSEGV (Segmentation vioalation) signal. Whats wrong? And is there a better way than mine to solve this task (most likely)
2
4042
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 store the data of a column from the database into the Combo box with the corresponding ID field (which is some other column of the same record).... -- Peter...
5
3430
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) _________________________________________ Function Age(DateOfBirth, DateToday) As Integer ' Returns the Age in years between 2 dates
51
4153
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
7084
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 action="http://cm1web1/WebSurveyComponents/script/ processform.asp" method="post">
3
34135
by: Ioannis Vranos | last post by:
Is printf("%ls") for printing wchar_t strings defined in C90, or it was added in C95?
0
10272
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10054
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9917
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7465
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6719
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5364
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3613
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2855
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.