Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > adding an integer to an stl string

Reply
Thread Tools

adding an integer to an stl string

 
 
exits funnel
Guest
Posts: n/a
 
      11-15-2003
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

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      11-15-2003

"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


 
Reply With Quote
 
 
 
 
exits funnel
Guest
Posts: n/a
 
      11-16-2003
> #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
>
>


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

-exits

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      11-16-2003

"Rolf Magnus" <> wrote in message
news:bp87cc$346$00$...
> 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


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      11-16-2003
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is there a way to AutoParse a string to another type - e.g. if aDate format then date, else if integer than Integer etc ????? Greg Hauptmann Ruby 6 08-06-2008 04:52 PM
Change a string to an integer, report an error if the string does not represent an integer? Randy Kramer Ruby 12 10-25-2007 09:56 PM
Counting occurances of string A in string B, and adding it to string B Sandman Perl Misc 7 08-03-2004 08:46 PM
Enumerating words and Adding integer, character and string dont bother Python 0 03-06-2004 03:15 AM
Append an integer to an stl string César C++ 5 01-22-2004 10:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57