Click to See Complete Forum and Search --> : cout can do better than printf?
fantasy2004
May 22nd, 2004, 09:20 AM
Hello,everyone!
I don't want to use printf of c style, i want to use cout. but i find i cant use cout as easy as printf.
for example, i dont know how to use cout to do something this:
printf("%14.6f%14.6f\n",1.0,2.31456);
I think it is a easy question, any suggestion is very helpful to me.
Thanks in advance.
sudhakarm
May 22nd, 2004, 10:19 AM
Try this ...
#include <iostream.h>
#include <iomanip.h>
void main()
{
float f1=1.0;
float f2=2.31456;
clrscr();
cout << endl;
cout.width(14);
cout.precision(6);
cout.setf(ios::showpoint);
cout << f1;
cout << endl;
cout.width(14);
cout.precision(6);
cout << f2;
// OR
cout << endl << setw(14) << setprecision(6) << f1;
cout << endl << setw(14) << setprecision(6) << f2;
getch();
}
fantasy2004
May 22nd, 2004, 08:32 PM
thank you,sudhakarm
i think your method is too complicated. In fact, I have use this kind method in my code, but i think it give me only complexity.
who can tell me how to implement cout as easy as printf?
such as:
float f1=1.0;
float f2=2.31456;
printf("%14.6f%14.6f\n",f1,f2);
i dont want to use cout like this:
cout << endl;
cout.width(14);
cout.precision(6);
cout.setf(ios::showpoint);
cout << f1;
cout << endl;
cout.width(14);
cout.precision(6);
cout << f2;
// OR
cout << endl << setw(14) << setprecision(6) << f1;
cout << endl << setw(14) << setprecision(6) << f2;
any suggestion will be helpful
TheCPUWizard
May 22nd, 2004, 09:29 PM
that is the proper way to use cout.
To quote (or at least paraphrase) Scott Meyers [author of Effective C++]:
If you dont like doing things the C++ way, please go program in another language. No one is forcing you to use C++, so quit complaining.
ps: That is probably my favorite quote from the entire book!
fantasy2004
May 22nd, 2004, 10:01 PM
Hi,TheCPUWizard
thanks for your reply first!
but i cant agree with your comment.
i think you give nothing but your feeling!
i have read the book of Scott Meyers , so i want to use c++ code style.
but when i want to use cout other than printf, it bring me more trouble than convenience.
So I doubt that if we can use cout instead of using printf?
just for keeping c++ style?
just think we do something like this:
fmt="%d %f %.......";
printf(fmt,a,b,c,d,e,f,g,h,i,j,k,...);
use cout????????
fogive me for my frankness.
wien
May 22nd, 2004, 10:04 PM
I for one can't understand why you feel "the C++ way" is adding to the complexity. Yes it is more typing for you to do, but the cout code is readable. Pass that printf line to a novice programmer (heck, even an experienced one), and he probably won't understand what the hey is going on. Do the same thing with the cout code, and everything is clear.
TheCPUWizard
May 22nd, 2004, 10:21 PM
Defiately use cout.
1) printf (s well as all other varg function) is error prown. The compiler can not check the type or even number of parameters match the format string.
2) For simple things cout MAY appear more complex. However, it is extremely flexible and extensible. As object complexity increases printf falls apart
class MyComplexObject {....}
MyComplexObject anInstance;
cout << anInstance << endl; // create printable dump of state
Not so easy to do that with printf
3) STL containers already provide high integration with streams.
4) Your program is probably going to use streams somewhere...be consistant use streams in all locations.
Finally, most aspects of design are "opinions" in the sense that there are many (infinite?) implementations that will function [at least sort of]. Experience has shown that some of these implementations have both long and short term benefits, while other have serious drawbacks. Having spent well over 12 years living C++, as well as reading the work (and even meeting with and discussing) of various authors, I feel that there is sufficient evidence supporting the use of streams over "C" library routines.
fantasy2004
May 22nd, 2004, 10:27 PM
thanks for your reply, wien
In fact, if we want to write data to file, we may need use so many printf with various format. In numerical computational, it is very common to encounter this case.
I read some codes on numerical computation these days, though written in c++, they use printf( in fact, it is fprintf ), I want to change it to cout( in fact , use fstream ). but i find it cant bring me more trouble than convience.
fantasy2004
May 22nd, 2004, 10:40 PM
thank you TheCPUWizard,
I know you are Elite Member. so your suggestion is of great value.
I agree with you that
"4) Your program is probably going to use streams somewhere...be consistant use streams in all locations"
that is the reason i want to disacrd all c style code such as printf.
but just as many authors' declaring that not using #define, i find the fact is other thing. In fact many famous software are famous for using macro.
so i doubt how many people use cout or fstream in numerical computation's output?
I mean "in fact" not in theory or in book.
TheCPUWizard
May 22nd, 2004, 10:56 PM
I know you are Elite Member. so your suggestion is of great value.
Thank you for the complement, but actually all it means is I talk alot :D Now the fact that I have successfully operated a software development consulting firm for the last 20 year might mean something....
we may need use so many printf with various format
You NEVER need to use printf...
In fact many famous software are famous for using macro.
Someone once quoted that 90% of everything is crap. Remember 50% of all software is below median level!. Seriously I know that they do, I also know a good number of companies who nearly forbid the use of #define [except for pre-processor control suce as include files]. Consider:
1) For about hlaf the time that C++ has been around many compilers did not support templates
2) Many people do not understand advanced C++ capabilities [do you know what a functor is?]
3) Software that is sold in source form [like many libraries or SDK's] need to be understood by the people who are buying them.
4) Old habits die hard
It is not suprising that there are many items which are not done in the cleanest, most reliable way. That is not a good enough reason [IMHO] to personally continue the trend.
There was a post in a recent thread that was very similar to this one. One of the other "guru's" provided a nice solution of some helper functions that did all of the formatting completely using streams, but left a cleaner syntax at the point where the output was being generalized [he encapsulated the details of formatting a stream, a good c++ practice]
fantasy2004
May 22nd, 2004, 11:18 PM
thank you very much,TheCPUWizard
I use fortran90 before, now i use c++. I think the c language will be completely replace by c++ someday. so i dont want to use the old c style syntax.
I think your suggestion is very helpful to me.
I have asked a question before about function pointer before,
so i know the something about functor.
i have an opion that different with yours: as i know, the VC++ use a set of strange macro (or famous macro), and i have see that the "fox lib" have use the similar macro, so i dont agree with you on this point. but i know little about c++, so my opion may be wrong.
There was a post in a recent thread that was very similar to this one. One of the other "guru's" provided a nice solution of some helper functions that did all of the formatting completely using streams, but left a cleaner syntax at the point where the output was being generalized [he encapsulated the details of formatting a stream, a good c++ practice]
would you give me the link of the article , i think it will be helpful to me.
thank you again, i expect i can ask you directly next time.
Guysl
May 23rd, 2004, 02:09 AM
I absolutely agree with TheCPUWizard.
There is a point, that Meyers raised in "MEC++ Item 23: Concider alternative libraries " -
which is the gain of C standard io lib speed in the cost of C++ iostream benefits - when performance is an issue.
wien
May 23rd, 2004, 10:21 AM
Originally posted by TheCPUWizard
...There was a post in a recent thread that was very similar to this one. One of the other "guru's" provided a nice solution of some helper functions that did all of the formatting completely using streams, but left a cleaner syntax at the point where the output was being generalized [he encapsulated the details of formatting a stream, a good c++ practice] Just to expand on this.
If you have a lot of the same formats being set time after time, you can always write your own format specifiers that set them all in one go:#include <iostream>
#include <iomanip>
std::ostream& your_format(std::ostream& p_stream)
{
p_stream << std::setw(14) << std::setprecision(6);
return p_stream;
}
class your_other_format
{
public:
your_other_format(unsigned int p_width)
:
m_width(p_width)
{}
private:
unsigned int m_width;
public:
friend std::ostream& operator <<
(std::ostream& p_stream, const your_other_format& p_object);
};
std::ostream& operator << (std::ostream& p_stream, const your_other_format& p_object)
{
p_stream << std::setw(p_object.m_width) << std::setprecision(6);
return p_stream;
}
int main()
{
float f1=1.0;
float f2=2.31456;
std::cout << your_format << f1 << std::endl;
std::cout << your_other_format(10) << f2 << std::endl;
system("pause");
return 0;
}If you name the modifiers properly, this technique can add to both ease of use and readabillity!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.