Problem using I/O manipulators

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Davis

    Problem using I/O manipulators

    I have some simple test code, which is copied pretty much verbatim
    from Josuttis (although he doesn't give complete examples):

    #include <iostream>
    main() {
    cout << hex << 1 << endl;
    std::cout << std::ios::hex << 1 << std::endl;
    }

    That's the whole program. The first line produces the correct output
    ('1'), but the second line produces '401'. Can anyone tell me what's
    going on here? This is on gcc 3.3.

    Many thanks

    Paul
    _______________ _______________ ________
    To get a valid mail address: s/m@/m1@/
  • Mike Wahler

    #2
    Re: Problem using I/O manipulators


    "Paul Davis" <mdhe51@dial.pi pex.com> wrote in message
    news:nd6eov8srr 54fmk2cdfm3bf3g mscg43cbd@4ax.c om...[color=blue]
    > I have some simple test code, which is copied pretty much verbatim
    > from Josuttis (although he doesn't give complete examples):
    >
    > #include <iostream>
    > main() {
    > cout << hex << 1 << endl;
    > std::cout << std::ios::hex << 1 << std::endl;
    > }
    >
    > That's the whole program. The first line produces the correct output
    > ('1'), but the second line produces '401'. Can anyone tell me what's
    > going on here? This is on gcc 3.3.[/color]

    std::ios::hex is a value (of type std::ios_base:: fmtflags),
    it's not a manipulator. So when you "print" it, you get
    its value (which may be different among standard library
    implementations ). 'std::hex' is a manipulator, which uses
    the value std::ios::hex to 'manipulate' the output
    formatting.

    This is explained in the Josuttis book.

    -Mike


    Comment

    • Rolf Magnus

      #3
      Re: Problem using I/O manipulators

      Paul Davis wrote:
      [color=blue]
      > I have some simple test code, which is copied pretty much verbatim
      > from Josuttis (although he doesn't give complete examples):
      >
      > #include <iostream>
      > main() {
      > cout << hex << 1 << endl;
      > std::cout << std::ios::hex << 1 << std::endl;
      > }
      >
      > That's the whole program. The first line produces the correct output
      > ('1'), but the second line produces '401'. Can anyone tell me what's
      > going on here? This is on gcc 3.3.[/color]

      I doubt that. The above program doesn't compile on gcc 3.3. Anyway,
      std::ios::hex is not the same as std::hex. The former is a flag, the
      latter a function.

      Comment

      • Paul Davis

        #4
        Re: Problem using I/O manipulators

        On Fri, 10 Oct 2003 23:14:17 +0200, Rolf Magnus <ramagnus@t-online.de>
        wrote:
        [color=blue]
        >Paul Davis wrote:
        >
        > <snipped>
        >I doubt that. The above program doesn't compile on gcc 3.3. Anyway,
        >std::ios::he x is not the same as std::hex. The former is a flag, the
        >latter a function.[/color]

        Whoops.. my fault. My g++ points to 2.96. This is the 3.3 version:

        #include <iostream>
        main() {
        std::cout << std::hex << 1 << std::endl;
        std::cout << std::ios::hex << 1 << std::endl;
        }

        std::hex works fine, std::ios::hex doesn't.

        Paul

        Comment

        • Mike Wahler

          #5
          Re: Problem using I/O manipulators


          "Paul Davis" <mdhe51@dial.pi pex.com> wrote in message
          news:tm9eov0tgp mhekbi52h34h043 gbcejkoq4@4ax.c om...[color=blue]
          > On Fri, 10 Oct 2003 23:14:17 +0200, Rolf Magnus <ramagnus@t-online.de>
          > wrote:
          >[color=green]
          > >Paul Davis wrote:
          > >
          > > <snipped>
          > >I doubt that. The above program doesn't compile on gcc 3.3. Anyway,
          > >std::ios::he x is not the same as std::hex. The former is a flag, the
          > >latter a function.[/color]
          >
          > Whoops.. my fault. My g++ points to 2.96. This is the 3.3 version:
          >
          > #include <iostream>
          > main() {
          > std::cout << std::hex << 1 << std::endl;
          > std::cout << std::ios::hex << 1 << std::endl;
          > }
          >
          > std::hex works fine, std::ios::hex doesn't.[/color]

          They both 'work fine'. The second output simply
          does not do what you (mistakenly) expect.

          Read Rolf's reply again, and mine.

          -Mike


          Comment

          • Paul Davis

            #6
            Re: Problem using I/O manipulators

            On Fri, 10 Oct 2003 21:11:32 GMT, "Mike Wahler"
            <mkwahler@mkwah ler.net> wrote:
            [color=blue]
            >
            >"Paul Davis" <mdhe51@dial.pi pex.com> wrote in message
            >news:nd6eov8sr r54fmk2cdfm3bf3 gmscg43cbd@4ax. com...[color=green]
            >> I have some simple test code, which is copied pretty much verbatim
            >> from Josuttis (although he doesn't give complete examples):
            >>
            >> #include <iostream>
            >> main() {
            >> cout << hex << 1 << endl;
            >> std::cout << std::ios::hex << 1 << std::endl;
            >> }
            >>
            >> That's the whole program. The first line produces the correct output
            >> ('1'), but the second line produces '401'. Can anyone tell me what's
            >> going on here? This is on gcc 3.3.[/color]
            >
            >std::ios::he x is a value (of type std::ios_base:: fmtflags),
            >it's not a manipulator. So when you "print" it, you get
            >its value (which may be different among standard library
            >implementation s). 'std::hex' is a manipulator, which uses
            >the value std::ios::hex to 'manipulate' the output
            >formatting.
            >
            >This is explained in the Josuttis book.
            >
            >-Mike[/color]

            Hmmm. Quoting verbatim from p622:
            [color=blue]
            > For example, the following statements write x and y in hexadecimal, and z in decimal:
            > int x,y,z;
            > ...
            > std::cout << std::ios::hex << x << std::endl;
            > std::cout << y << ' ' << std::ios::dec << z << std::endl;[/color]

            There's an example further down the page where he uses 'std::hex'
            instead. Looks like I'm not the only one who got confused.. :)

            Do you happen to know if there's a better reference for formatting?
            I'm trying to implement a subset of printf, and there dont seem to be
            any manipulators or flags to set an integer precision, for example.

            Thanks

            Paul

            Comment

            • Mike Wahler

              #7
              Re: [links] Problem using I/O manipulators

              "Paul Davis" <mdhe51@dial.pi pex.com> wrote in message
              news:tr9eovg64f ovp4df4au1pb19c d4qfdtt08@4ax.c om...[color=blue]
              > On Fri, 10 Oct 2003 21:11:32 GMT, "Mike Wahler"
              > <mkwahler@mkwah ler.net> wrote:
              >[color=green]
              > >
              > >"Paul Davis" <mdhe51@dial.pi pex.com> wrote in message
              > >news:nd6eov8sr r54fmk2cdfm3bf3 gmscg43cbd@4ax. com...[color=darkred]
              > >> I have some simple test code, which is copied pretty much verbatim
              > >> from Josuttis (although he doesn't give complete examples):
              > >>
              > >> #include <iostream>
              > >> main() {
              > >> cout << hex << 1 << endl;
              > >> std::cout << std::ios::hex << 1 << std::endl;
              > >> }
              > >>
              > >> That's the whole program. The first line produces the correct output
              > >> ('1'), but the second line produces '401'. Can anyone tell me what's
              > >> going on here? This is on gcc 3.3.[/color]
              > >
              > >std::ios::he x is a value (of type std::ios_base:: fmtflags),
              > >it's not a manipulator. So when you "print" it, you get
              > >its value (which may be different among standard library
              > >implementation s). 'std::hex' is a manipulator, which uses
              > >the value std::ios::hex to 'manipulate' the output
              > >formatting.
              > >
              > >This is explained in the Josuttis book.
              > >
              > >-Mike[/color]
              >
              > Hmmm. Quoting verbatim from p622:
              >[color=green]
              > > For example, the following statements write x and y in hexadecimal, and[/color][/color]
              z in decimal:[color=blue][color=green]
              > > int x,y,z;
              > > ...
              > > std::cout << std::ios::hex << x << std::endl;
              > > std::cout << y << ' ' << std::ios::dec << z << std::endl;[/color]
              >
              > There's an example further down the page where he uses 'std::hex'
              > instead. Looks like I'm not the only one who got confused.. :)[/color]

              Alas, people are not perfect. :-)

              Visit http://www.josuttis.com/libbook/
              Click on the "Errata" link, and search for "622"

              You might want to peruse the other errata items as well.
              [color=blue]
              > Do you happen to know if there's a better reference for formatting?[/color]

              The material in the Josuttis book is imo enough for
              'basic' use of i/o formatting, but there is another
              excellent book which has a much "deeper" treatment
              of all things IOStream:


              [color=blue]
              > I'm trying to implement a subset of printf, and there dont seem to be
              > any manipulators or flags to set an integer precision, for example.[/color]

              What do you mean by "integer precision"? "Precision"
              only applies to floating point values.

              -Mike


              Comment

              Working...