"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:

stringstream oss;
oss << s << i;
std::string output(oss.str());
std::cout << output << '\n'; /* prints Hello42 */
return 0;
}
-Mike