whats the difference between /n and endl?
whats the difference between /n and endl?
nothing
all it does is prevent extra shift keying
cout << somevariable << endl;
instead of
cout << somevariable << "\n";
\n is an escape sequence.
And endl does the same thing but its a standatd strem thingie.
I really dont know the technicalities. Im sure someone does.
example
Code:cout << "Line one\nLine two"; cout << "Line one" << endl << "Line Two";
What is C++?
ok, there is a difference....
endl is a global function which returns '\n'
<< is an overloaded operator which tells the ostream class cout how to handle the data type that follows
i believe endl also calls flush to flush the stream
"Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
-- Cowper
Operating Systems=Slackware Linux 9.1,Windows 98/Xp
Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)
You may teach a person from now until doom's day, but that person will only know what he learns himself.
Now I know what doesn't work.
A problem is understood by solving it, not by pondering it.
For a bit of humor check out xkcd web comic http://xkcd.com/235/
ok....when did a stream come into the picture?
endl is a manipulator in the ostream class. When inserted into an output stream it inserts a newline character then flushes the stream. It is cheaper to just use "\n" then to use endl because flushing the stream unnecessarily can slow down your program.
"Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
-- Cowper
Operating Systems=Slackware Linux 9.1,Windows 98/Xp
Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)
You may teach a person from now until doom's day, but that person will only know what he learns himself.
Now I know what doesn't work.
A problem is understood by solving it, not by pondering it.
For a bit of humor check out xkcd web comic http://xkcd.com/235/
a stream came into the picutre you you put
#include <iostream>
in your program
Actually I believe NiVaG was referring to your seemingly irrelevant explanation of the insertion operator.a stream came into the picutre you you put
#include <iostream>
in your program
when using \n be sure to use single quotes to insert the character. When you use double quotes it creates a null terminated c-string which will cost you an extra sizeof(char)Originally Posted by misplaced
Here is a sample implementation of endl:
When talking about endl, you are implying streams as endl is an operator to ostream (a stream class). So there is definitely relevance there, adn as stated before endl does a flush() after "\n".Code:template<class _Elem, class _Traits> inline basic_ostream<_Elem, _Traits>& __cdecl endl(basic_ostream<_Elem, _Traits>& _Ostr) { // insert newline and flush stream _Ostr.put(_Ostr.widen('\n')); _Ostr.flush(); return (_Ostr); }
Sometimes when I use cin.get() to pause the program before ending (keep the console open long enough to read it) it doesn't work by itself and I have to precede it with cin.ignore(). I understand that this is because there is still a \n in the input buffer....maybe? Would using endl, since it flushes the stream, make it so that I wouldn't have to enter cin.ignore()? Or are they completely unrelated things?
Couldn't you use cout.flush() and cin.flush() before cin.get()?Originally Posted by Elhaz
Child who knows C++Using Borland C/C++ Compiler 5.5 (Command Line Version)
I don' know. That's the first time I've seen those. I'll check them out. Thanks.Couldn't you use cout.flush() and cin.flush() before cin.get()?
Since endl flushes the output stream, it wouldn't help much in clearing the input stream.
I believe flush is only defined for output streams, so it wouldn't work for cin.