Hi, everybody.

I am reviewing some old code and two questions came to mind.

1) Is it better to use “\n” or endl?
"\n" seems to be working just fine, but most code samples I see in books nowadays use endl. What is the difference?

2) For multi-line output statements, is it better to avoid repeating the cout part of the statement?

For example, at the beginning of the program, some instructions are output to the user; a sample block follows:

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";

Is it better to write this block as follows (one big long single cout statement)?

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";

Recommended Answers

All 5 Replies

Hi, everybody.

I am reviewing some old code and two questions came to mind.

1) Is it better to use “\n” or endl?
"\n" seems to be working just fine, but most code samples I see in books nowadays use endl. What is the difference?

\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-line cout myself.

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.

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.

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.