Recently the idea of which one is better has arose on both the boards in one of my posts, and in my class. Both told me i shouldn't use "\n" but rather use <<endl;
What is your opinion?
Printable View
Recently the idea of which one is better has arose on both the boards in one of my posts, and in my class. Both told me i shouldn't use "\n" but rather use <<endl;
What is your opinion?
Seems like endl both breaks the line and flushes the buffer.Quote:
ostream _FAR & _RTLENTRY _EXPFUNC endl(ostream _FAR &); // insert newline and flush
ostream _FAR & _RTLENTRY endl(ostream _FAR &); // insert newline and flush
Personally, I use endl in C++. Not that I know it's better, but cause I've always done so :).
Plus, it looks better with ... << endl << ...
than ... << "\n" << ...
I suppose you could do:
#define endl "\n"
or:
#define endl "\n" << flush
So in other words, endl; would me much more effecient in a lengthy program and is better practice?
I always just use both. I use \n when the end is in quotes
cout<<somevar<<"Text in quotes \n" ;
and endl when it is not
cout<<"Text in quotes"<<someVar<<endl;
DitoQuote:
Originally posted by Magos
Personally, I use endl in C++. Not that I know it's better, but cause I've always done so :).
>So in other words, endl; would me much more effecient in a lengthy program and is better practice?
Actually, endl would probably be less efficient per call than \n. But that kind of performance nitpicking is a bad thing in most programs, so I just use endl if I need to flush the stream.
If I need a single newline and flush:
std::cout<<std::endl;
If I need two newlines and a flush:
std::cout<<'\n'<<std::endl;
If I need a newline, flush not needed:
std::cout<<'\n';
-Prelude
How do you know when you do(n't) need to flush the line?
I might be wrong, but:Quote:
Originally posted by Ride -or- Die
How do you know when you do(n't) need to flush the line?
It is when you flush the buffer that the text actually appears on the screen. If you print:
cout << "Hello" << flush << "World" << flush;
"Hello" would be printed on the screen a little bit before "World", causing flicker (probably unnoticable to a human eye, but still...).
ok what mod 999'd the poll :P
What are you talking about ROD?
thats it i'm going to start taking screenshots!
LOL! :D
Screen shots? Are you implying that you don't trust the people and mods here at CP.com? Are you halucinating again? Mayeb you should talk to someone proffessionally about this ROD, wouldn't want anything to happen to you, like loosing it for example.
..........
endl because it not only skips to the next line but clears the buffer, keeping things nice and neat