how to print on screen in realtime

Z

zl2k

hi, all
I am trying to moniter the runing of a program by printing out some
characters in a line at each stage. say,

//program part1
cout<<"part1 ";
//program part2
cout<<part2 ";
....

I was expecting the program will pring "part1 part2 part3 part4 ...."
in realtime but instead it only print out when the program is finished.
I force it to print out by adding <<endl for each cout then the output
turns to vertical which is hard to see. My question is, how may I print
it in a line in realtime? Thanks.

zl2k
 
V

Victor Bazarov

zl2k said:
hi, all
I am trying to moniter the runing of a program by printing out some
characters in a line at each stage. say,

//program part1
cout<<"part1 ";
//program part2
cout<<part2 ";
...

I was expecting the program will pring "part1 part2 part3 part4 ...."
in realtime but instead it only print out when the program is
finished. I force it to print out by adding <<endl for each cout then
the output turns to vertical which is hard to see. My question is,
how may I print it in a line in realtime? Thanks.

Read about 'flush' manipulator. If it doesn't work, use your platform's
capabilities instead.

V
 
A

Adi

cout << flush;
cout.flush();
cout << endl;

This will flush the buffered output to be printed. The output is
buffered and will be printed when it reaches a limit or when the
program ends or some other condition like that.

Adi
 
M

Marcus Kwok

zl2k said:
I am trying to moniter the runing of a program by printing out some
characters in a line at each stage. say,

//program part1
cout<<"part1 ";
//program part2
cout<<part2 ";
...

I was expecting the program will pring "part1 part2 part3 part4 ...."
in realtime but instead it only print out when the program is finished.
I force it to print out by adding <<endl for each cout then the output
turns to vertical which is hard to see. My question is, how may I print
it in a line in realtime? Thanks.

I don't know about "realtime", but you can try adding std::flush instead
of std::endl.

// program part1
cout << "part1 " << flush;
// program part2
cout << "part2 " << flush;
 
R

Robbie Hatley

zl2k said:
I am trying to moniter the runing of a program by printing out some
characters in a line at each stage. say,

//program part1
cout<<"part1 ";
//program part2
cout<<part2 ";
...

I was expecting the program will pring "part1 part2 part3 part4 ...."
in realtime but instead it only print out when the program is finished.
I force it to print out by adding <<endl for each cout then the output
turns to vertical which is hard to see. My question is, how may I print
it in a line in realtime? Thanks.

As Victor Bazarov rightly pointed out, you can use flush at each point
where you want to force output, like so:

//program part1
cout << "part1 " << flush;
//program part2
cout << "part2 " << flush;

Another thing you can use is the "endl" manipulator, which first
prints an endline character ('\n'), then flushes the stream.
I tend to write text a line at a time in my progrms with immediate
output, like so:


//program part1
cout << "part1" << endl;
//program part2
cout << "part2" << endl;

which prints:

part1
part2
part3
etc.

in real-time.

I just thought I'd mention that option.


--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 
M

Marcus Kwok

Robbie Hatley said:
Another thing you can use is the "endl" manipulator, which first
prints an endline character ('\n'), then flushes the stream.
I tend to write text a line at a time in my progrms with immediate
output, like so:


//program part1
cout << "part1" << endl;
//program part2
cout << "part2" << endl;

which prints:

part1
part2
part3
etc.

in real-time.

I just thought I'd mention that option.

The OP had already considered that option but found the output to be too
hard to see.
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top