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
newbie alert.
/n is the same thing as endl he was aksing a prefrence the onyl differ is that you put /n inside the quotes endl is a seperate thing
/n example->
cout << "hello mom /n";
cout << "hello dad";
endl example->
cout << "hello mom" << endl;
cout << "hello dad";
the end resualt sshould be the same
No, as was stated before:Quote:
Originally posted by Klinerr1
newbie alert.
/n is the same thing as endl he was aksing a prefrence the onyl differ is that you put /n inside the quotes endl is a seperate thing
/n example->
cout << "hello mom /n";
cout << "hello dad";
endl example->
cout << "hello mom" << endl;
cout << "hello dad";
the end resualt sshould be the same
endl != \n
endl == \n + Flush
yah, whos the newb now punk ;)
<<endl;
simply cause i get lazy and its easier for this ol' fart to type that than other.
PEACE OUT!
cj
:eek: :eek: :eek: :eek: :eek: :eek: ah well as someone with tons of brain power once said "learn something new a day"
lol if they had tons of brain power, they might have used a lil grammar and said "Learn something new every day"
i use newl - which simply returns '\n'
I read some performance issues on gcc2.95
in which '\n' was 60 times faster than endl - so I thought, why use endl??
a flush is made when the buffer is full - in most cases that's enough.
Only sometime (if I write to files) it is important to flush, because the program may crash and then the file would be empty...
I don't think you can say which is better because they are different.
Simply if you need to flush the buffer use endl, if you don't use '\n'.
it's that simple.
I have never really had much of a need to flush the buffer, so I just use them like this:
If it is a string, use \n, if it is after a variable, use endl.
Ex:
cout << "Hello, World! \n";
cout << myVar << endl;
Even though endl flushes the buffer, that is not taught in schools. The only thing that is taught in schools is that it goes to the next line, and so that is what people use it for. That is what I have always used it for, and so that's how I use \n and endl.
but..... I thought if you wanted to flush something (which solves all IO problems) you use fflush(stdin); Am I mistaken?Quote:
Originally posted by ammar
I don't think you can say which is better because they are different.
Simply if you need to flush the buffer use endl, if you don't use '\n'.
it's that simple.
:D :D :D
i'll use endl then...
btw, newb alert yourself :P
All this discussion and noone has mentioned that stdout is unbuffered, hence no need for endl unless couting to a file, no?
but how will you know if you're outputting to a file or to a screen or whatever? all you know is that it's stdout.Quote:
Originally posted by Sebastiani
All this discussion and noone has mentioned that stdout is unbuffered, hence no need for endl unless couting to a file, no?
Well, obviously. But in most cases, stout is directed to a terminal, and anyway, if otherwise, the programmer would know in advance (hopefully!).
when i use C++ Ostreams which is very rare i use 'endl', other wise i use '\n'.
>All this discussion and noone has mentioned that stdout is unbuffered
since when? i think this is imp dependant.
not even close. you never used linux much i assume (windoze users dont do this sort of thing much), but stdin and stdout are regularly redirected all over the placeQuote:
Originally posted by Sebastiani
Well, obviously. But in most cases, stout is directed to a terminal, and anyway, if otherwise, the programmer would know in advance (hopefully!).
no-one, I have to know where you get those avatars. The dancing dog was hilarious and the panda is totally cute. :)
-Prelude
I agree with Prelude on the avatar.
And one more thing, Some times the buffer is automaticaly flushed, for example when you write:
cin >> "blabla";
the buffer is automaticaly flushed.
>I have to know where you get those avatars.
trade secrets.