undesired cout behaviour

G

George Bell

OK BIG newbie question here.
I've been trying to write cout statements in a C++ program compiled with
gcc. The problem I'm having with the cout statements is that I can' get
anything to print unless there's a newline on the end:

cout << "Hello World"; or
cout << "Hello World\r"; or
cout << "Hello World" << '\r';

print absolutely nothing
while

cout << "Hello World\n"

prints "Hello World"

I'd like to print without the newline. Is it possible?

George
 
R

Rob Williscroft

George Bell wrote in in
comp.lang.c++:

cout << "Hello World\n"

prints "Hello World"

I'd like to print without the newline. Is it possible?

Try:

#include <iostream>

int main()
{
std::cout << "Hello Usnet";
std::cout.flush();
}

How your console works (and indeed *if* you have one) is platform
specific. However the best gaurantee that you get all your output
is to send a final newline '\n' *and* to flush the stream, the
output manipulator std::endl, does that 'all in one'.

HTH.

Rob.
 
R

Rolf Magnus

George said:
OK BIG newbie question here.
I've been trying to write cout statements in a C++ program compiled
with gcc. The problem I'm having with the cout statements is that I
can' get anything to print unless there's a newline on the end:

Probably because your terminal is line buffered and won't flush the
buffer unless a newline is encountered.
cout << "Hello World"; or
cout << "Hello World\r"; or
cout << "Hello World" << '\r';

Try:

cout << "Hello World" << flush;
 
G

George Bell

Large thanks for the ideas.
Yeah, I am running the program on a terminal in a linux platform.
Bash shell,
$TERM=linux.

George
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top