By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

adding an integer to an stl string

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"exits funnel" <ex*********@NOSPAMyahoo.com> wrote in message
news:3F**************@NOSPAMyahoo.com...
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::ostringstream oss;
oss << s << i;
std::string output(oss.str());
std::cout << output << '\n'; /* prints Hello42 */
return 0;
}

-Mike
Jul 19 '05 #2

P: n/a
> #include <iostream>
#include <sstream>
#include <string>

int main()
{
std::string s("Hello");
int i(42);
std::ostringstream 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

Jul 19 '05 #3

P: n/a
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 ;-)
Jul 19 '05 #4

P: n/a

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bp*************@news.t-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
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.