By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,192 Members | 867 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

[C++] Arrays and endl

P: n/a
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;
}
---------------------------------------------------
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Penna Elabi <te*********@go.com> writes:
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.
Jul 22 '05 #2

P: n/a
Penna Elabi wrote in
news:77**************************@posting.google.c om:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

P: n/a
Penna Elabi wrote:
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';

Jul 22 '05 #4

P: n/a
In article <77**************************@posting.google.com >,
Penna Elabi <te*********@go.com> wrote:
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.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #5

P: n/a
On 24 Nov 2003 05:56:25 -0800, te*********@go.com (Penna Elabi) wrote:
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
Jul 22 '05 #6

P: n/a
"Andreas Kirkeskov Carlsen" <ak*@daimi.au.dk> wrote in message news:<bp**********@news.net.uni-c.dk>...

Penna Elabi <te*********@go.com> writes:
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.
Jul 22 '05 #7

P: n/a
jt*******@presby.edu (Jon Bell) wrote in message news:<bp**********@jtbell.presby.edu>...

In article <77**************************@posting.google.com >,
Penna Elabi <te*********@go.com> wrote:
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.


Thanks.
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.