terminal progress indicator

K

Koen Janssens

Can anybody give me an example on how to write a progress indicator in C++
using the standard library cerr or cout?

My problem is I do not know how reset the output stream to the beginning of
the line, so just that information will do fine.

Thanks - Koen
 
A

Ali R.

I don't think that c++ has anything like that. You could possibly use
either a carriage return char(13) (without the line feed) or use backspace
char(8) to backspace your way back to the end of the line (backspace doesn't
remove the character from the screen, it only moves the cursor back you
would have to print a space to remove the char). to go back one char cout <<
char(8) << char(' ') << char(8).
If you're program is going to only run on a PC you can use a bit of assembly
to move the cursor around on the screen. There are a couple of functions
for interrupt 21 that let you control the cursor.

Ali R.
 
V

Victor Bazarov

Koen Janssens said:
Can anybody give me an example on how to write a progress indicator in C++
using the standard library cerr or cout?

My problem is I do not know how reset the output stream to the beginning of
the line, so just that information will do fine.

Outputting '\n' is a common way to begin a new line (the name of the
character is "newline"). However, there is no system-independent way
to "reset to output stream to the beginning of the line". Many of
the systems respond to '\r' as you probably expect, but that's _not_
guaranteed to be portable.

Victor
 
D

David Fisher

Koen Janssens said:
Can anybody give me an example on how to write a progress indicator in C++
using the standard library cerr or cout?

My problem is I do not know how reset the output stream to the beginning of
the line, so just that information will do fine.

Use a carriage return ("\r") instead of a newline ("\n")
 
V

Victor Bazarov

Ali R. said:
I don't think that c++ has anything like that. You could possibly use
either a carriage return char(13) (without the line feed) or use backspace
char(8) to backspace your way back to the end of the line (backspace doesn't
remove the character from the screen, it only moves the cursor back you
would have to print a space to remove the char). to go back one char cout <<
char(8) << char(' ') << char(8).

Just a comment: code-independent names of those characters are \r
and \b, respectively. They are only 13 and 8 in ASCII, and ASCII
is not the only code around. And not on all systems they produce
the described effect, mind you.
If you're program is going to only run on a PC you can use a bit of assembly
to move the cursor around on the screen. There are a couple of functions
for interrupt 21 that let you control the cursor.

Being on PC is not enough for that. You're apparently talking of
[MS-]DOS, a horribly outdated and notoriously dangerous platform,
or any of its emulators. Perhaps you should move your discussion
to comp.os.msdos.programmer...
 
M

Mike Wahler

Koen Janssens said:
Can anybody give me an example on how to write a progress indicator in C++
using the standard library cerr or cout?

My problem is I do not know how reset the output stream to the beginning of
the line, so just that information will do fine.

#include <iostream>
#include <string>
#include <time.h>

void delay(double seconds)
{
time_t start(time(0));

if(start != -1)
while((seconds - difftime(time(0), start)))
;
}

void reset(unsigned int count)
{
std::cout << '\r'
<< std::string(count, ' ')
<< '\r';
}

int main()
{
const unsigned int passes(3);
const unsigned int stars(10);
char c(0);

for(unsigned int i = 0; i < passes; ++i)
{
for(unsigned int j = 0; j < stars; ++j)
{
delay(1);
std::cout << (c = '0' + i + 1);
}

reset(stars);
}

return 0;
}


-Mike
 
P

Peter Koch Larsen

"Mike Wahler" <[email protected]> skrev i en meddelelse

[snip]
int main()
{
const unsigned int passes(3);
const unsigned int stars(10);
char c(0);

for(unsigned int i = 0; i < passes; ++i)
{
for(unsigned int j = 0; j < stars; ++j)
{
delay(1);
std::cout << (c = '0' + i + 1);
}

reset(stars);
}

return 0;
}


-Mike

Just out of curiosity.... what is this "reset(stars);" about - i do not
understand it and haven't seen it before.

/Peter
 
T

Thomas Matthews

Koen said:
Can anybody give me an example on how to write a progress indicator in C++
using the standard library cerr or cout?

My problem is I do not know how reset the output stream to the beginning of
the line, so just that information will do fine.

Thanks - Koen

In days before windowing, my fellow programmers would output the
sequence:
'|'
"\b \b/"
"\b \b-"
"\b \b\\"
also known as a propeller.

Simpler versions use "*", "\b ", "\b*", ...
This is known as a heartbeat indicator.

Usually in the embedded world, we pursuade the hardware
folk to give us an LED to play with and use that as a
heartbeat indicator.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
A

Ali R.

Victor Bazarov said:
cout

Just a comment: code-independent names of those characters are \r
and \b, respectively. They are only 13 and 8 in ASCII, and ASCII
is not the only code around. And not on all systems they produce
the described effect, mind you.


Thanks for point that out.

If you're program is going to only run on a PC you can use a bit of assembly
to move the cursor around on the screen. There are a couple of functions
for interrupt 21 that let you control the cursor.

Being on PC is not enough for that. You're apparently talking of
[MS-]DOS, a horribly outdated and notoriously dangerous platform,
or any of its emulators. Perhaps you should move your discussion
to comp.os.msdos.programmer...

You are absolutely correct. If it's only going to run on a windows based
machine, SetConsoleCursorPosition would be a much better choice.
 
M

Mike Wahler

Peter Koch Larsen said:
"Mike Wahler" <[email protected]> skrev i en meddelelse

[snip]
int main()
{
const unsigned int passes(3);
const unsigned int stars(10);
char c(0);

for(unsigned int i = 0; i < passes; ++i)
{
for(unsigned int j = 0; j < stars; ++j)
{
delay(1);
std::cout << (c = '0' + i + 1);
}

reset(stars);
}

return 0;
}


-Mike

Just out of curiosity.... what is this "reset(stars);" about - i do not
understand it and haven't seen it before.

It's just a function I wrote. I sends a carriage return
followed by a specified number of space characters to the
output stream. Nothing 'magical' about it.

-Mike
 
P

Peter Koch Larsen

Mike Wahler said:
Peter Koch Larsen said:
"Mike Wahler" <[email protected]> skrev i en meddelelse

[snip]
int main()
{
const unsigned int passes(3);
const unsigned int stars(10);
char c(0);

for(unsigned int i = 0; i < passes; ++i)
{
for(unsigned int j = 0; j < stars; ++j)
{
delay(1);
std::cout << (c = '0' + i + 1);
}

reset(stars);
}

return 0;
}


-Mike

Just out of curiosity.... what is this "reset(stars);" about - i do not
understand it and haven't seen it before.

It's just a function I wrote. I sends a carriage return
followed by a specified number of space characters to the
output stream. Nothing 'magical' about it.

-Mike

Hi Mike....

Just reread your original answer - and there it was. I must have been blind!

Thanks
Peter
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top