Printing progressbar

Discussion in 'C++' started by H?vard Sj?voll, Jun 24, 2004.

  1. 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-
     
    H?vard Sj?voll, Jun 24, 2004
    #1
    1. Advertising

  2. H?vard Sj?voll

    Sree Guest

    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/
     
    Sree, Jun 24, 2004
    #2
    1. Advertising

  3. H?vard Sj?voll

    Unforgiven Guest

    "H?vard Sj?voll" <> wrote in message
    news:...
    > 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
     
    Unforgiven, Jun 24, 2004
    #3
  4. 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
     
    Victor Bazarov, Jun 24, 2004
    #4
  5. H?vard Sj?voll

    Allan Bruce Guest

    "Victor Bazarov" <> wrote in message
    news:9WBCc.1836$...
    > 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
     
    Allan Bruce, Jun 24, 2004
    #5
  6. H?vard Sj?voll

    Mike Weller Guest

    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 << "]";
    //----------------------------
     
    Mike Weller, Jun 24, 2004
    #6
  7. H?vard Sj?voll

    Mike Weller Guest

    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 << "]";
    //----------------------------
     
    Mike Weller, Jun 24, 2004
    #7
  8. On Thu, 24 Jun 2004 16:45:44 +0100, Allan Bruce wrote:

    >
    > "Victor Bazarov" <> wrote in message
    > news:9WBCc.1836$...
    >> 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.
     
    Owen Jacobson, Jun 25, 2004
    #8
  9. H?vard Sj?voll

    Unforgiven Guest

    "Owen Jacobson" <> wrote in message
    news:p...
    > On Thu, 24 Jun 2004 16:45:44 +0100, Allan Bruce wrote:
    >
    >>
    >> "Victor Bazarov" <> wrote in message
    >> news:9WBCc.1836$...
    >>> 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
     
    Unforgiven, Jun 28, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maka Sili
    Replies:
    0
    Views:
    517
    Maka Sili
    Aug 25, 2004
  2. Maka Sili
    Replies:
    0
    Views:
    411
    Maka Sili
    Aug 25, 2004
  3. Mehr H
    Replies:
    1
    Views:
    645
    Chris Jackson
    Nov 6, 2003
  4. Nilton Volpato
    Replies:
    0
    Views:
    399
    Nilton Volpato
    May 8, 2006
  5. Nilton Volpato
    Replies:
    0
    Views:
    450
    Nilton Volpato
    May 7, 2006
Loading...

Share This Page