adding an integer to an stl string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • exits funnel

    adding an integer to an stl string

    Hello,

    I'm trying to append the ascii representation of an integer to an stl
    string and I'm having some trouble. I've got the following bits of code:

    #include <string>
    #include <cstdlib>

    ....

    char buf[10];
    itoa(14, buf, 10);
    string m_name = "House #" + buf;

    When I try to compile this, my compiler complains about 'itoa' being an
    undeclared function and in fact grepping stdlib.h verifies that the
    funcion is not to be found. I've been away from c for some time and am
    now trying to learn c++. Two questions then:

    1) Where can I find atoi or some equivalent function?
    2) Is there an easier way to add an integer to the string?

    Thanks in advance for any replies!

    -exits

  • Mike Wahler

    #2
    Re: adding an integer to an stl string


    "exits funnel" <exitsfunnel@NO SPAMyahoo.com> wrote in message
    news:3FB682EE.3 070807@NOSPAMya hoo.com...[color=blue]
    > Hello,
    >
    > I'm trying to append the ascii representation of an integer to an stl
    > string and I'm having some trouble. I've got the following bits of code:
    >
    > #include <string>
    > #include <cstdlib>
    >
    > ...
    >
    > char buf[10];
    > itoa(14, buf, 10);[/color]

    There is no such function as 'itoa()' in standard C++.
    [color=blue]
    > string m_name = "House #" + buf;[/color]

    Undefined behavior. The array 'buf' was never initialized.
    [color=blue]
    >
    > When I try to compile this, my compiler complains about 'itoa' being an
    > undeclared function and in fact grepping stdlib.h verifies that the
    > funcion is not to be found.[/color]

    Right, it's not part of C++.
    [color=blue]
    >I've been away from c for some time and am
    > now trying to learn c++. Two questions then:
    >
    > 1) Where can I find atoi or some equivalent function?[/color]

    The standard function 'atoi()' is declared by standard header
    <stdlib.h> or <cstdlib>. But note that this function will not
    do what you're asking about.

    [color=blue]
    > 2) Is there an easier way to add an integer to the string?[/color]

    Yes, use a stringstream. See below.
    [color=blue]
    >
    > Thanks in advance for any replies!
    >
    > -exits
    >[/color]

    #include <iostream>
    #include <sstream>
    #include <string>

    int main()
    {
    std::string s("Hello");
    int i(42);
    std::ostringstr eam oss;
    oss << s << i;
    std::string output(oss.str( ));
    std::cout << output << '\n'; /* prints Hello42 */
    return 0;
    }

    -Mike


    Comment

    • exits funnel

      #3
      Re: adding an integer to an stl string

      > #include <iostream>[color=blue]
      > #include <sstream>
      > #include <string>
      >
      > int main()
      > {
      > std::string s("Hello");
      > int i(42);
      > std::ostringstr eam oss;
      > oss << s << i;
      > std::string output(oss.str( ));
      > std::cout << output << '\n'; /* prints Hello42 */
      > return 0;
      > }
      >
      > -Mike
      >
      >[/color]

      Thanks Mike, this is exactly what I was looking for.

      -exits

      Comment

      • Rolf Magnus

        #4
        Re: adding an integer to an stl string

        Mike Wahler wrote:
        [color=blue][color=green]
        >> #include <string>
        >> #include <cstdlib>
        >>
        >> ...
        >>
        >> char buf[10];
        >> itoa(14, buf, 10);[/color]
        >
        > There is no such function as 'itoa()' in standard C++.
        >[color=green]
        >> string m_name = "House #" + buf;[/color]
        >
        > Undefined behavior. The array 'buf' was never initialized.[/color]

        itoa() was supposed to initialize it ;-)


        Comment

        • Mike Wahler

          #5
          Re: adding an integer to an stl string


          "Rolf Magnus" <ramagnus@t-online.de> wrote in message
          news:bp87cc$346 $00$2@news.t-online.com...[color=blue]
          > Mike Wahler wrote:
          >[color=green][color=darkred]
          > >> #include <string>
          > >> #include <cstdlib>
          > >>
          > >> ...
          > >>
          > >> char buf[10];
          > >> itoa(14, buf, 10);[/color]
          > >
          > > There is no such function as 'itoa()' in standard C++.
          > >[color=darkred]
          > >> string m_name = "House #" + buf;[/color]
          > >
          > > Undefined behavior. The array 'buf' was never initialized.[/color]
          >
          > itoa() was supposed to initialize it ;-)[/color]

          Yes, oops, sorry. But 'itoa()' might have failed, though. :-)

          -Mike


          Comment

          Working...