Re: A small game
On Sep 17, 2:31 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
James Kanze wrote:
You should use endl until you understand buffering issues.
Given the level of the posting, he's far from that yet.
I find that a bit contradictory with what you wrote
immediately after:
There's no relationship. What I wrote afterwards is additional
information.
Also, by default, std::cin and std::cout are tied, so any
attempt to read on std::cin will flush the buffer on
std::cout.
Given that the most usual reason for flushing std::cout is so
that the text will be displayed before the user is asked for
some input with std::cin, and given that, as you say,
std::cout *is* automatically flushed when std::cin is used,
there doesn't seem to be too many reasons to use std::endl.
The usual reason for flushing an ostream (not just cout, any
ostream) is to ensure that the data has been output. In
practice, very few programs write to the console, and even less
read exclusively from std::cin. Unless you know what you are
doing, some of the data you think you've written will in fact be
in the buffers, and not have been output. This can have many
consequences: the most obvious are when someone is reading the
data using tail -f, or when the program crashes for some
reason.
The biggest problem with std::endl is precisely that it
flushes the output stream.
And how is that a real problem? It's purely a performance
issue, and doesn't affect most programs at all.
If a new user is taught that std::endl is good and should
always be used by default, he will learn that bad habit,
If a new user is taught to only use '\n', he will learn that bad
habit, and wonder why he's not getting the expected output.
endl is never wrong. Not flushing when a flush is needed is
wrong. Without a certain amount of experience, and
understanding buffering, you don't know when a flush is needed;
using endl isn't perfect, but if your output is line oriented,
it's close enough. And once you understand the issues, you'll
stop programming by habit, think about it, and do the right
thing. (Which is still to use endl 99% of the time; I use it by
default even today, and I pretty much understand the issues.)
and it will carry on a long time. At some point he will start
creating programs which write enormous amounts of (text) data
to an output file, and by custom he will use std::endl to
write newlines to it. If these newlines are very common in
this output, in most systems the writing will be slowed down
by a rather big factor.
That sounds like vacuous speculation to me. I've been writing
C++ for more than fifteen years now; I regularly use endl, as a
default, and I've never had a performance problem because of it.
(Hint: on real systems, most large output goes to a data base,
not to an ordinary file. With one notable exception: our log
files often reach several gigabytes. But a log file is
precisely where you absolutely must flush after each write.)
He might not even realize that something is wrong, and could
think that that writing speed is normal when in fact it's
completely hindered by the constant needless flushing.
More likely, if he gets into the habit of using "\n", he'll use
it when writing log files as well, which will cause no end of
problems when the program crashes, and someone tries to debug
it.
IMO the difference between "\n" and std::endl can and should
be taught from the very beginning, and the recommendation
given that "\n" should be used by default.
And that's just wrong. Experienced programmers use endl by
default, and most of the time, don't even worry about the
difference.
Just say something like "if at some point you are outputting
some text with std::cout and then your program pauses or
whatever and the text is not appearing on screen, output an
std::endl or std::flush to force it to show the text".
Also when creating CLI applications which might require
showing some progress percentage, this "trick" is useful:
std::cout << "\rProgress: " << percentage << "%" <<
std::flush;
Anytime your output isn't in an entire block. About the only
time "\n" is acceptable is when you're outputting in a loop (and
do a flush after the loop), or when you have a long sequence of
output statements (and end them with an endl or a flush).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34