954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Output: Two Questions

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]


2) For multi-line output statements, is it better to avoid repeating the cout part of the statement?
[INDENT]For example, at the beginning of the program, some instructions are output to the user; a sample block follows:[/INDENT]

cout << "\nThe first entry of this file must be the degree, N, of the polynomial for\n";
cout << "which the roots are to be calculated.\n";
cout << "Entries for the coefficients of the polynomial should follow, starting with\n";
cout << "the coefficient for the highest power of x and working down to the coefficient\n";

[INDENT]Is it better to write this block as follows (one big long singlecout statement)?[/INDENT]

cout << "\nThe first entry of this file must be the degree, N, of the polynomial for\n"
        << "which the roots are to be calculated.\n"
        << "Entries for the coefficients of the polynomial should follow, starting with\n"
        << "the coefficient for the highest power of x and working down to the coefficient\n";
DavidB
Posting Whiz in Training
213 posts since Jul 2006
Reputation Points: 48
Solved Threads: 10
 

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
Moderator
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.

Zvjezdan23
Junior Poster
154 posts since Dec 2010
Reputation Points: -3
Solved Threads: 5
 
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
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

All my colleges have been using \n. But once when using formatting (Output formating functions from iomanip) functions, \n gave me error by counting itself in. Then causing the program to show wrong formatting(Logical mistake here). Since then i have sticked to endl, sadly none of my colleges followed..... So i would say endl.....

2nd question, answer depends on individual programmer......I would go for multi-line, since its much more cleaner.....

Captain Jake
Posting Whiz in Training
209 posts since Nov 2011
Reputation Points: 12
Solved Threads: 11
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You