terminal progress indicator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Koen Janssens

    terminal progress indicator

    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

  • Ali R.

    #2
    Re: terminal progress indicator

    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" <kgjanss@sandia .gov> wrote in message
    news:BBE927F3.2 E2D%kgjanss@san dia.gov...[color=blue]
    > 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[/color]
    of[color=blue]
    > the line, so just that information will do fine.
    >
    > Thanks - Koen
    >[/color]


    Comment

    • Victor Bazarov

      #3
      Re: terminal progress indicator

      "Koen Janssens" <kgjanss@sandia .gov> wrote...[color=blue]
      > 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[/color]
      of[color=blue]
      > the line, so just that information will do fine.[/color]

      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


      Comment

      • David Fisher

        #4
        Re: terminal progress indicator

        "Koen Janssens" <kgjanss@sandia .gov> wrote in message
        news:BBE927F3.2 E2D%kgjanss@san dia.gov...[color=blue]
        > 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[/color]
        of[color=blue]
        > the line, so just that information will do fine.[/color]

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


        Comment

        • Victor Bazarov

          #5
          Re: terminal progress indicator

          "Ali R." <nospam@company .com> wrote...[color=blue]
          > 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[/color]
          doesn't[color=blue]
          > 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[/color]
          <<[color=blue]
          > char(8) << char(' ') << char(8).[/color]

          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.
          [color=blue]
          > If you're program is going to only run on a PC you can use a bit of[/color]
          assembly[color=blue]
          > to move the cursor around on the screen. There are a couple of functions
          > for interrupt 21 that let you control the cursor.[/color]

          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.p rogrammer...
          [color=blue]
          >
          > Ali R.
          >
          > "Koen Janssens" <kgjanss@sandia .gov> wrote in message
          > news:BBE927F3.2 E2D%kgjanss@san dia.gov...[color=green]
          > > Can anybody give me an example on how to write a progress indicator in[/color][/color]
          C++[color=blue][color=green]
          > > using the standard library cerr or cout?
          > >
          > > My problem is I do not know how reset the output stream to the beginning[/color]
          > of[color=green]
          > > the line, so just that information will do fine.
          > >
          > > Thanks - Koen
          > >[/color]
          >
          >[/color]


          Comment

          • Mike Wahler

            #6
            Re: terminal progress indicator


            "Koen Janssens" <kgjanss@sandia .gov> wrote in message
            news:BBE927F3.2 E2D%kgjanss@san dia.gov...[color=blue]
            > 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[/color]
            of[color=blue]
            > the line, so just that information will do fine.[/color]

            #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(cou nt, ' ')
            << '\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


            Comment

            • Peter Koch Larsen

              #7
              Re: terminal progress indicator


              "Mike Wahler" <mkwahler@mkwah ler.net> skrev i en meddelelse
              news:fxRwb.1480 1$n56.12102@new sread1.news.pas .earthlink.net. ..

              [snip]
              [color=blue]
              > 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
              >
              >[/color]

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

              /Peter


              Comment

              • Thomas Matthews

                #8
                Re: terminal progress indicator

                Koen Janssens wrote:
                [color=blue]
                > 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
                >[/color]

                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:

                C++ Faq: http://www.parashift.com/c++-faq-lite
                C Faq: http://www.eskimo.com/~scs/c-faq/top.html
                alt.comp.lang.l earn.c-c++ faq:

                Other sites:
                http://www.josuttis.com -- C++ STL Library book

                Comment

                • Ali R.

                  #9
                  Re: terminal progress indicator


                  "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
                  news:SjRwb.2300 97$9E1.1254971@ attbi_s52...[color=blue]
                  > "Ali R." <nospam@company .com> wrote...[color=green]
                  > > 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[/color][/color]
                  backspace[color=blue][color=green]
                  > > char(8) to backspace your way back to the end of the line (backspace[/color]
                  > doesn't[color=green]
                  > > 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[/color][/color]
                  cout[color=blue]
                  > <<[color=green]
                  > > char(8) << char(' ') << char(8).[/color]
                  >
                  > 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.
                  >[/color]


                  Thanks for point that out.

                  [color=blue][color=green]
                  > > If you're program is going to only run on a PC you can use a bit of[/color]
                  > assembly[color=green]
                  > > to move the cursor around on the screen. There are a couple of[/color][/color]
                  functions[color=blue][color=green]
                  > > for interrupt 21 that let you control the cursor.[/color]
                  >
                  > 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.p rogrammer...
                  >[/color]

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




                  Comment

                  • Mike Wahler

                    #10
                    Re: terminal progress indicator


                    "Peter Koch Larsen" <pkl@mailme.d k> wrote in message
                    news:3fc4821f$0 $95095$edfadb0f @dread11.news.t ele.dk...[color=blue]
                    >
                    > "Mike Wahler" <mkwahler@mkwah ler.net> skrev i en meddelelse
                    > news:fxRwb.1480 1$n56.12102@new sread1.news.pas .earthlink.net. ..
                    >
                    > [snip]
                    >[color=green]
                    > > 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
                    > >
                    > >[/color]
                    >
                    > Just out of curiosity.... what is this "reset(star s);" about - i do not
                    > understand it and haven't seen it before.[/color]

                    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


                    Comment

                    • Peter Koch Larsen

                      #11
                      Re: terminal progress indicator


                      "Mike Wahler" <mkwahler@mkwah ler.net> skrev i en meddelelse
                      news:OKSxb.1814 5$n56.17018@new sread1.news.pas .earthlink.net. ..[color=blue]
                      >
                      > "Peter Koch Larsen" <pkl@mailme.d k> wrote in message
                      > news:3fc4821f$0 $95095$edfadb0f @dread11.news.t ele.dk...[color=green]
                      > >
                      > > "Mike Wahler" <mkwahler@mkwah ler.net> skrev i en meddelelse
                      > > news:fxRwb.1480 1$n56.12102@new sread1.news.pas .earthlink.net. ..
                      > >
                      > > [snip]
                      > >[color=darkred]
                      > > > 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
                      > > >
                      > > >[/color]
                      > >
                      > > Just out of curiosity.... what is this "reset(star s);" about - i do not
                      > > understand it and haven't seen it before.[/color]
                      >
                      > 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
                      >[/color]

                      Hi Mike....

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

                      Thanks
                      Peter


                      Comment

                      Working...