<< "\t";

M

Michael

just a quickie:

as
cout << endl;
is to
cout << "\n";

is there an equivalent for
cout << "\t";

??
Thanks
Mike
 
D

David Harmon

On Fri, 14 May 2004 15:46:29 +0000 (UTC) in comp.lang.c++, "Michael"
as
cout << endl;
is to
cout << "\n";

is there an equivalent for
cout << "\t";

No! And if there was, it would be even more evil than cout<<endl;
 
J

Jean Charbonneau

Michael said:
just a quickie:

as
cout << endl;
is to
cout << "\n";

is there an equivalent for
cout << "\t";

??
Thanks
Mike

cout << endl; is not the same as cout << "\n"; because the first one besides
doing a new line, flushes the output as well
 
I

Ivan Vecerina

Michael said:
just a quickie:

as
cout << endl;
is to
cout << "\n";

First of all, one should prefer to use
cout << '\n' instead of cout << "\n"
(avoiding unnecessary pessimization).

Then, the purpose of endl is to end a line AND
flush the output buffer. It is equivalent to:
cout << '\n';
cout.flush();
This is useful in several logging or communication
protocols (although the console output streams,
by default, are not buffered in standard C++).
is there an equivalent for
cout << "\t";
NB: again, prefer '\t'

How often does one want to flush the output
after writing a tab character ?



Regards,
Ivan
 
H

Howard

David Harmon said:
On Fri, 14 May 2004 15:46:29 +0000 (UTC) in comp.lang.c++, "Michael"


No! And if there was, it would be even more evil than cout<<endl;

What's evil about endl? I use it a lot. It's especially helpful when
debugging stuff, because it flushes the buffer and I know that there's
nothing in the stream that I can't yet see in my console window.

-Howard
 
M

Marc

Howard" said:
What's evil about endl? I use it a lot. It's especially helpful when
debugging stuff, because it flushes the buffer and I know that there's
nothing in the stream that I can't yet see in my console window.

Why not something like:

#ifdef DEBUG
std::cout.rdbuf().setbuf(0,0);
#endif

?
 
H

Howard

Marc said:
Why not something like:

#ifdef DEBUG
std::cout.rdbuf().setbuf(0,0);
#endif

?

Why? It's a lot more work than using endl instead of '\n' (especially with
a lot of cout's being used), and you haven't given any indication as to what
if anything is wrong with endl.

Also, just because I'm "debugging", doesn't mean it's a debug build. Often
times there are problems that only show up in release builds, and it helps a
lot to be sure that everything that was written to the stream was flushed to
the console prior to the problem occurring that I'm trying to solve.

Again...what's wrong with endl???

-Howard
 
J

John Harrison

Michael said:
just a quickie:

as
cout << endl;
is to
cout << "\n";

is there an equivalent for
cout << "\t";

??
Thanks
Mike

No, but it's easy enough to write your own. Call it 'endt' say

#include <iostream>
using namespace std;

ostream& endt(ostream& os)
{
return os << '\t' << flush;
}

int main()
{
cout << "abc" << endt;
cout << "def" << endl;
}

john
 
I

Ivan Vecerina

Howard said:
What's evil about endl? I use it a lot. It's especially helpful when
debugging stuff, because it flushes the buffer and I know that there's
nothing in the stream that I can't yet see in my console window.

Note: console I/O is unbuffered in standard C++.
So std::endl should be unnecessary, though not really evil IMO.

Regards,
Ivan
 
J

John Carson

John Harrison said:
No, but it's easy enough to write your own. Call it 'endt' say

#include <iostream>
using namespace std;

ostream& endt(ostream& os)
{
return os << '\t' << flush;
}

int main()
{
cout << "abc" << endt;
cout << "def" << endl;
}

john


Nice. And if you don't really want the flushing, it is easier still.

#include <iostream>
using namespace std;

const char tab='\t';

int main()
{
cout << "abc" << tab;
cout << "def" << endl;
}
 
W

Wouter Lievens

Michael said:
just a quickie:

as
cout << endl;
is to
cout << "\n";

is there an equivalent for
cout << "\t";

??
Thanks
Mike


std::endl is a function, not a character.
 
I

Ivan Vecerina

Jeff Schwab said:
Where does the standard say that?

I slipped here, my comment was not totally correct.

cerr is unbuffered, and should indeed be used for debugging stuff.

Regarding cout, it is a bit more complicated... it is unlikely to
be buffered at the C++ level because by default it is supposed
to be synchronized with stdout. But it can still be buffered.

The following post/thread provides a description of the situation:
http://groups.google.com/[email protected]


Thanks, and best regards,
Ivan
 
J

Jeff Schwab

Ivan said:
I slipped here, my comment was not totally correct.

cerr is unbuffered, and should indeed be used for debugging stuff.

Regarding cout, it is a bit more complicated... it is unlikely to
be buffered at the C++ level because by default it is supposed
to be synchronized with stdout. But it can still be buffered.

Why would that make it unlikely to be buffered? I've never seen a
situation in which cout was unbuffered.
 
I

Ivan Vecerina

Jeff Schwab said:
Why would that make it unlikely to be buffered? I've never seen a
situation in which cout was unbuffered.

Please read the thread I refered to in my previous post:
http://groups.google.com/[email protected]

Unless the program calls std::ios_base::sync_with_stdio(false),
it is possible to mix calls that write to stdout and cout with
the guarantee of a properly synchronized output.
I.e: cout<<'a'; putchar('b'); cout<<'c'<<endl;
Is required to output "abc" (and not "bac" or anything else).

Because, on most platforms, cout is implemented in terms of stdout,
this means that buffering must be disabled in cout - at the C++ level.
An implementation-defined buffering mechanism of stdout may still
apply, however.

Regards,
Ivan
 
J

Jeff Schwab

Ivan said:
emphasis on: ~~~~~~~~~~~~~~~~



Please read the thread I refered to in my previous post:
http://groups.google.com/[email protected]

Unless the program calls std::ios_base::sync_with_stdio(false),
it is possible to mix calls that write to stdout and cout with
the guarantee of a properly synchronized output.
I.e: cout<<'a'; putchar('b'); cout<<'c'<<endl;
Is required to output "abc" (and not "bac" or anything else).

That doesn't make it unbuffered; in fact, it generally means that cout
is buffered.
Because, on most platforms, cout is implemented in terms of stdout,
this means that buffering must be disabled in cout - at the C++ level.

Not sure what you mean. If you mean that the C++ run-time library may
not provide redundant buffering when the OS already provides a buffer
for stdout, then OK, I'm with you.
 
I

Ivan Vecerina

Jeff Schwab said:
Not sure what you mean. If you mean that the C++ run-time library may
not provide redundant buffering when the OS already provides a buffer
for stdout, then OK, I'm with you.

This is indeed what I meant by:
Regarding cout, it is a bit more complicated... it is unlikely to
be buffered at the C++ level because by default it is supposed
to be synchronized with stdout. But it can still be buffered.


Peace,
Ivan
 

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
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top