I need to build a string by adding an int value to an existing string.
string s="blah blah";
int num=9;
s=s+num;
doesn't work...
I already tried using itos but I can't get it to work...
Concatenmating an int to a string in C++?
how about sprintf as in
string s="blah blah"
string buffer;
int num=9
sprintf(buffer, "%s%d", s, num);
Reply:You are not initializing string correctly,
string s="whatever";
this is wrong way to initialize a string!!!
right way is,
char s[20]="blah blah";
and i would also like to inform you that there is no need to convert integer to string because string can store numeric and character values both.
To convert string to integer:
num=atoi(s);
To convert string to float:
num=atof(s);
Reply:You have to add like to like.
If you google for "Convert integer to string C++"
you can choose the best way to code this on your system.
Reply:In windows, you can use _itoa to convert an int to a character array. Other systems have something similar.
int num = 9;
string s("blah blah");
char buff[10]; //to hold the output of itoa
::_itoa(num, buff, 10); //10 is base 10.
s += buff;
blazing star
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment