the thing with endl is that it also flushes the ouput-buffer, making sure that anything in the output-buffer is printed/written/something else. To achieve the same with \n you would have do a << flush aswell.
the thing with endl is that it also flushes the ouput-buffer, making sure that anything in the output-buffer is printed/written/something else. To achieve the same with \n you would have do a << flush aswell.
STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.
Why are other instructors teachingOriginally Posted by swgh
void main()They don't know any better! I believe your instructor is correct (for the reason Shakti pointed out), but if he taught any of the above, any 'never'/'always' statement would be questionable IMAO.
fflush(stdin);
gets();
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
>> Daved, if what you say is true then why was I taught at college in C++ to use endl and only use "\n" when the need was justified?
First, many, many things that are taught by instructors, books, tutorials and even forum posters are based on habit and experience rather than current best practice. Apparently, before C++ was standardized, you were allowed to use the endl when you included <iostream.h>, so most everybody did. Then when the standard was made, in what may have just been an oversight, they defined the language in such a way that you weren't guaranteed to get endl when you included <iostream>. But all the standard library writers continued to allow it (possibly because so much existing code relied on it).
So all the people in the habit of using endl with iostream just continued to do so. I personally still use endl without including <ostream> because that was how I was taught as well. It is an extremely trivial issue because most (or all) compilers allow it.
That said, the teaching and reference tools should be as correct as possible. So the wiki page should not use endl without <ostream>, and your instructor should also teach the use of either '\n' or endl with <ostream>. In many cases, the buffer will be flushed automatically making endl unnecessary, but there are times when it is useful. In those times, you should #include <ostream> to be completely correct, but I don't think I would call someone out on it if they didn't.
Thanks Daved, that has cleared it up in my mind