Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Printing progressbar

Reply
Thread Tools

Printing progressbar

 
 
H?vard Sj?voll
Guest
Posts: n/a
 
      06-24-2004
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-
 
Reply With Quote
 
 
 
 
Sree
Guest
Posts: n/a
 
      06-24-2004

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

--
Use our news server 'news.foorum.com' from anywhere.
More details at: http://nnrpinfo.go.foorum.com/
 
Reply With Quote
 
 
 
 
Unforgiven
Guest
Posts: n/a
 
      06-24-2004
"H?vard Sj?voll" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.

--
Unforgiven

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-24-2004
H?vard Sj?voll wrote:
> 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
 
Reply With Quote
 
Allan Bruce
Guest
Posts: n/a
 
      06-24-2004

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:9WBCc.1836$(E-Mail Removed)...
> H?vard Sj?voll wrote:
> > 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


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


 
Reply With Quote
 
Mike Weller
Guest
Posts: n/a
 
      06-24-2004
Victor Bazarov wrote:

> H?vard Sj?voll wrote:
>
>> 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


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 << "]";
//----------------------------
 
Reply With Quote
 
Mike Weller
Guest
Posts: n/a
 
      06-24-2004
Victor Bazarov wrote:

> H?vard Sj?voll wrote:
>
>> 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


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 << "]";
//----------------------------

 
Reply With Quote
 
Owen Jacobson
Guest
Posts: n/a
 
      06-25-2004
On Thu, 24 Jun 2004 16:45:44 +0100, Allan Bruce wrote:

>
> "Victor Bazarov" <(E-Mail Removed)> wrote in message
> news:9WBCc.1836$(E-Mail Removed)...
>> H?vard Sj?voll wrote:
>> > 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

>
> 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>

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

 
Reply With Quote
 
Unforgiven
Guest
Posts: n/a
 
      06-28-2004
"Owen Jacobson" <(E-Mail Removed)> wrote in message
newsan.2004.06.25.05.41.38.561061@lionsanctuary. net...
> On Thu, 24 Jun 2004 16:45:44 +0100, Allan Bruce wrote:
>
>>
>> "Victor Bazarov" <(E-Mail Removed)> wrote in message
>> news:9WBCc.1836$(E-Mail Removed)...
>>> H?vard Sj?voll wrote:
>>> > 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

>>
>> 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>


Yup. The bug was fixed in Windows 2000 SP3 and Windows XP SP1. In NT4 it'll
remain a bug forever.

--
Unforgiven

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM
ANN: progressbar 2.2 - Text mode progressbar for console applications Nilton Volpato Python 0 05-08-2006 05:28 AM
ANN: progressbar 2.2 - Text mode progressbar for console applications Nilton Volpato Python 0 05-07-2006 07:47 PM



Advertisments