Hi, everybody.
I am reviewing some old code and two questions came to mind.
1) Is it better to use “\n” or endl?[INDENT]"\n" seems to be working just fine, but most code samples I see in books nowadays use endl. What is the difference?[/INDENT]
\n simply adds a newline to the output buffer.
endl adds a newline and flushes the buffer (forces the output to be displayed).
Use ENDL.
2) For multi-line output statements, is it better to avoid repeating the cout part of the statement?
I think that's up to the programmer's personal preference.
I'd tend to use the multi-linecout myself.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I have to agree with WaltP in his assessment of this question, for both parts. In any case, DO NOT use a \n in a C++ output statement, unless you don't mind it getting mixed up with other output to the same device (such as a terminal or log file). That why in C we have to use fflush(stream) after an fprintf(stream, "fmt", args, ...). FWIW, this rule is relaxed somewhat when the output is to cerr (c++) or stderr (c) since they are by design unbuffered.
rubberman
Posting Virtuoso
1,564 posts since Mar 2010
Reputation Points: 277
Solved Threads: 179
endl, in my opinion, helps skip lines in the code. It also helps organize code because when you cin >> x_integer;
cout << endl; or cout << endl << endl; (skips 2 lines)
can make your code look cleaner and more organized. In my opinion anyways.
I have no idea what you are trying to say.
Skipping lines in code is a bad thing. You don't want to skip lines of code, you want to execute every line.
cout < endl; doesn't do anything to your code. It only affects your output.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944