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

terminal progress indicator

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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.

"Koen Janssens" <kg*****@sandia.gov> wrote in message
news:BBE927F3.2E2D%kg*****@sandia.gov...
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

Jul 22 '05 #2

P: n/a
"Koen Janssens" <kg*****@sandia.gov> wrote...
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
Jul 22 '05 #3

P: n/a
"Koen Janssens" <kg*****@sandia.gov> wrote in message
news:BBE927F3.2E2D%kg*****@sandia.gov...
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")
Jul 22 '05 #4

P: n/a
"Ali R." <no****@company.com> wrote...
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...

Ali R.

"Koen Janssens" <kg*****@sandia.gov> wrote in message
news:BBE927F3.2E2D%kg*****@sandia.gov...
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


Jul 22 '05 #5

P: n/a

"Koen Janssens" <kg*****@sandia.gov> wrote in message
news:BBE927F3.2E2D%kg*****@sandia.gov...
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
Jul 22 '05 #6

P: n/a

"Mike Wahler" <mk******@mkwahler.net> skrev i en meddelelse
news:fx*******************@newsread1.news.pas.eart hlink.net...

[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
Jul 22 '05 #7

P: n/a
Koen Janssens wrote:
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

Jul 22 '05 #8

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:SjRwb.230097$9E1.1254971@attbi_s52...
"Ali R." <no****@company.com> wrote...
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.

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.


Jul 22 '05 #9

P: n/a

"Peter Koch Larsen" <pk*@mailme.dk> wrote in message
news:3f***********************@dread11.news.tele.d k...

"Mike Wahler" <mk******@mkwahler.net> skrev i en meddelelse
news:fx*******************@newsread1.news.pas.eart hlink.net...

[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
Jul 22 '05 #10

P: n/a

"Mike Wahler" <mk******@mkwahler.net> skrev i en meddelelse
news:OK*******************@newsread1.news.pas.eart hlink.net...

"Peter Koch Larsen" <pk*@mailme.dk> wrote in message
news:3f***********************@dread11.news.tele.d k...

"Mike Wahler" <mk******@mkwahler.net> skrev i en meddelelse
news:fx*******************@newsread1.news.pas.eart hlink.net...

[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
Jul 22 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.