adding an integer to an stl string

Discussion in 'C++' started by exits funnel, Nov 15, 2003.

  1. exits funnel

    exits funnel Guest

    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
     
    exits funnel, Nov 15, 2003
    #1
    1. Advertising

  2. exits funnel

    Mike Wahler Guest

    "exits funnel" <> wrote in message
    news:...
    > 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);


    There is no such function as 'itoa()' in standard C++.

    > string m_name = "House #" + buf;


    Undefined behavior. The array 'buf' was never initialized.

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


    Right, it's not part of C++.

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


    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.


    > 2) Is there an easier way to add an integer to the string?


    Yes, use a stringstream. See below.

    >
    > Thanks in advance for any replies!
    >
    > -exits
    >


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

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

    -Mike
     
    Mike Wahler, Nov 15, 2003
    #2
    1. Advertising

  3. exits funnel

    exits funnel Guest

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


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

    -exits
     
    exits funnel, Nov 16, 2003
    #3
  4. exits funnel

    Mike Wahler Guest

    "Rolf Magnus" <> wrote in message
    news:bp87cc$346$00$-online.com...
    > Mike Wahler wrote:
    >
    > >> #include <string>
    > >> #include <cstdlib>
    > >>
    > >> ...
    > >>
    > >> char buf[10];
    > >> itoa(14, buf, 10);

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

    > >
    > > Undefined behavior. The array 'buf' was never initialized.

    >
    > itoa() was supposed to initialize it ;-)


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

    -Mike
     
    Mike Wahler, Nov 16, 2003
    #4
  5. exits funnel

    Rolf Magnus Guest

    Mike Wahler wrote:

    >> #include <string>
    >> #include <cstdlib>
    >>
    >> ...
    >>
    >> char buf[10];
    >> itoa(14, buf, 10);

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

    >
    > Undefined behavior. The array 'buf' was never initialized.


    itoa() was supposed to initialize it ;-)
     
    Rolf Magnus, Nov 16, 2003
    #5
    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. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,976
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. César
    Replies:
    5
    Views:
    20,559
    César
    Jan 22, 2004
  3. dont bother
    Replies:
    0
    Views:
    284
    dont bother
    Mar 6, 2004
  4. Randy Kramer
    Replies:
    12
    Views:
    396
    Robert Klemme
    Oct 25, 2007
  5. Sandman
    Replies:
    7
    Views:
    223
    Anno Siegel
    Aug 3, 2004
Loading...

Share This Page