Printing progressbar

H

H?vard Sj?voll

Anyone got any code for how to print a progressbar in a console-window?

That is, - something like

"Loading: X%"

where X is continousely updated.

-håvard-
 
S

Sree

Standard C++ doesn't offer any way to do it. You have to refer to your system
related console programming manuals for it.
 
U

Unforgiven

H?vard Sj?voll said:
Anyone got any code for how to print a progressbar in a console-window?

That is, - something like

"Loading: X%"

for( int x = 0; x <= 100; ++x )
{
std::cout << "Loading: " << x << "%\r" << std::flush;
// do something
}

\r without \n sends the cursor back to the beginning of the line but not to
a new line, so you can overwrite the previous contents of the line. This is
afaik the only standards-compliant portable way to do it.
 
V

Victor Bazarov

H?vard Sj?voll said:
Anyone got any code for how to print a progressbar in a console-window?

That is, - something like

"Loading: X%"

where X is continousely updated.

Something like

std::cout << "\rLoading: " << Xvalue << '%';

The behaviour of your terminal when \r is printed to it is of course
OS-specific, but it usually works the way you want it.

Don't forget to print 'std::endl' when you're done to move to the next
line.

Victor
 
A

Allan Bruce

Victor Bazarov said:
Something like

std::cout << "\rLoading: " << Xvalue << '%';

The behaviour of your terminal when \r is printed to it is of course
OS-specific, but it usually works the way you want it.

Don't forget to print 'std::endl' when you're done to move to the next
line.

Victor

If this doesnt work, another way I found was to use several \b - so
basically:

for (int i=0; i<LengthOfProgressInChars; i++)
std::cout << "\b";
std::cout << "\rLoading: " << Xvalue << '%';

Allan
 
M

Mike Weller

Victor said:
Something like

std::cout << "\rLoading: " << Xvalue << '%';

The behaviour of your terminal when \r is printed to it is of course
OS-specific, but it usually works the way you want it.

Don't forget to print 'std::endl' when you're done to move to the next
line.

Victor

Yeah I think the best way to do this is with your final progress bar
dislpayed as, for example:

[0%...25%...50%...75%...100%]

And progressivly add a '.' or number to the end. Something like this:

//----------------------------
cout << "[";

for ( int x = 0; x <= 100; x++ )
{
if ( !( x % 20 ) ) // display every 20th number
cout << x << "%";
else if ( !( x % 5) ) // otherwise display a dot every 5 numbers
cout << ".";
}

cout << "]";
//----------------------------
 
M

Mike Weller

Victor said:
Something like

std::cout << "\rLoading: " << Xvalue << '%';

The behaviour of your terminal when \r is printed to it is of course
OS-specific, but it usually works the way you want it.

Don't forget to print 'std::endl' when you're done to move to the next
line.

Victor

Yeah I think the best way to do this is with your final progress bar
dislpayed as, for example:

[0%...25%...50%...75%...100%]

And progressivly add a '.' or number to the end. Something like this:

//----------------------------
cout << "[";

for ( int x = 0; x <= 100; x++ )
{
if ( !( x % 20 ) ) // display every 20th number
cout << x << "%";
else if ( !( x % 5) ) // otherwise display a dot every 5 numbers
cout << ".";
}

cout << "]";
//----------------------------
 
O

Owen Jacobson

If this doesnt work, another way I found was to use several \b - so
basically:

for (int i=0; i<LengthOfProgressInChars; i++)
std::cout << "\b";
std::cout << "\rLoading: " << Xvalue << '%';

<platform-specific behaviour>

Watch it with that. If you go too far backwards, some versions of Windows
(NT-based ones) will bluescreen spectacularly.

<http://homepages.tesco.net/~J.deBoynePollard/FGA/csrss-backspace-bug.html>

</platform-specific behaviour>
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top