[C++] Arrays and endl

P

Penna Elabi

In the below Linux C++ program, why do I need endl commands to see the
numbers in array exampleArray? I mean that if I remove the endl
commands, the program doesn't print anything -- why not?

---------------------------------------------------
#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
string exampleArray[2][2] = {{"0","1"},{"1","0"}};

cout << exampleArray[0][0] << endl;
cout << exampleArray[1][0] << endl;
cout << exampleArray[0][1] << endl;
cout << exampleArray[1][1] << endl;
return EXIT_SUCCESS;
}
---------------------------------------------------
 
G

Grumble

Penna said:
In the below Linux C++ program, why do I need endl commands to see the
numbers in array exampleArray? I mean that if I remove the endl
commands, the program doesn't print anything -- why not?

Even after the program exits?
#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
string exampleArray[2][2] = {{"0","1"},{"1","0"}};

cout << exampleArray[0][0] << endl;
cout << exampleArray[1][0] << endl;
cout << exampleArray[0][1] << endl;
cout << exampleArray[1][1] << endl;
return EXIT_SUCCESS;
}

If I am not mistaken, the expression cout << endl calls endl(cout)
which writes '\n' to cout, then flushes it.

Try this:

cout << exampleArray[0][0];
cout << exampleArray[0][1];
cout << exampleArray[1][0];
cout << exampleArray[1][1] << '\n';
 
A

Andreas Kirkeskov Carlsen

Penna Elabi said:
In the below Linux C++ program, why do I need endl commands to see the
numbers in array exampleArray? I mean that if I remove the endl
commands, the program doesn't print anything -- why not?

---------------------------------------------------
#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
string exampleArray[2][2] = {{"0","1"},{"1","0"}};

cout << exampleArray[0][0] << endl;
cout << exampleArray[1][0] << endl;
cout << exampleArray[0][1] << endl;
cout << exampleArray[1][1] << endl;
return EXIT_SUCCESS;
}
---------------------------------------------------

What compiler are you using ?
The described behaviour may be caused by a stream not flushing.
The program acts as expected both with and without endl when compiled using
g++ version 3.3.1.
 
R

Rob Williscroft

Penna Elabi wrote in
In the below Linux C++ program, why do I need endl commands to see the
numbers in array exampleArray? I mean that if I remove the endl
commands, the program doesn't print anything -- why not?

---------------------------------------------------
#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
string exampleArray[2][2] = {{"0","1"},{"1","0"}};

cout << exampleArray[0][0] << endl;
cout << exampleArray[1][0] << endl;
cout << exampleArray[0][1] << endl;
cout << exampleArray[1][1] << endl;
return EXIT_SUCCESS;
}
---------------------------------------------------

std::endl does 2 things firstly it sends a newline '\n' to the stream
and then it flushes the stream's buffer, its only when the stream's
buffer is flushed (or it gets full) that data is actually writen to
the undelying file (or in this case your console).

Some systems have an additional layer of buffering where only complete
lines are writen to the console. You may have such a system.

#include <iostream>
int main()
{
std::cout << "single line ";
std::cout.flush();
}

On most sytems this will display a single line of text, the OS might
insert a newline after the programme has finnished, but that is beyond
the control of your programme. On some other systems nothing will be
displayed at all.

HTH.

Rob.
 
J

Jon Bell

In the below Linux C++ program, why do I need endl commands to see the
numbers in array exampleArray?

Probably because your implementation doesn't flush output buffers at the
end of a program. endl inserts a newline in the output stream, and then
flushes the buffer so any accumulated output shows up at its destination.

The system I tried your example on just now (g++ 3.3, Mac OS X) does
produce output both with and without the endl's. Of course, without them
the output appears all on one line.
 
T

tom_usenet

In the below Linux C++ program, why do I need endl commands to see the
numbers in array exampleArray? I mean that if I remove the endl
commands, the program doesn't print anything -- why not?

Sometimes consoles will only display anything when you write a new
line.
---------------------------------------------------
#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
string exampleArray[2][2] = {{"0","1"},{"1","0"}};

cout << exampleArray[0][0] << endl;
cout << exampleArray[1][0] << endl;
cout << exampleArray[0][1] << endl;
cout << exampleArray[1][1] << endl;

Instead try:
cout << exampleArray[0][0];
cout << exampleArray[1][0];
cout << exampleArray[0][1];
cout << exampleArray[1][1];
cout << '\n';


return EXIT_SUCCESS;
}

cout is flushed at program exit (or by explicit flushes, or by endl),
but some consoles will only be happy with complete lines of output -
any left over part line might be discarded.

Tom
 
P

Penna Elabi

Andreas Kirkeskov Carlsen said:
Penna Elabi said:
In the below Linux C++ program, why do I need endl commands to see the
numbers in array exampleArray? I mean that if I remove the endl
commands, the program doesn't print anything -- why not?

---------------------------------------------------
#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
string exampleArray[2][2] = {{"0","1"},{"1","0"}};

cout << exampleArray[0][0] << endl;
cout << exampleArray[1][0] << endl;
cout << exampleArray[0][1] << endl;
cout << exampleArray[1][1] << endl;
return EXIT_SUCCESS;
}
---------------------------------------------------

What compiler are you using ?
The described behaviour may be caused by a stream not flushing.
The program acts as expected both with and without endl when compiled using
g++ version 3.3.1.

I have the same g++ version: 3.3.1.
 
P

Penna Elabi

Probably because your implementation doesn't flush output buffers at the
end of a program. endl inserts a newline in the output stream, and then
flushes the buffer so any accumulated output shows up at its destination.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top