Concatenating an integer to a string
I have a function that take char*
I call this function in an array, and I need to pass the following:- i+" binomial" to be like "2 binomial" or whatever value of i value (i is an integer) it is pretty easy to be done in java, but how to concatenate an integer with a string and get char* or string to pass to a new function... If you missed me i just need to get the following : string =integer + string thanks |
In the tools I use, I can only concatenate an integer and a string to a string. If you are using some sort of scripting language, you often can return that latter string, i.e. in PHP you can use:
<?php function concat($i,$string){ $new_string=$i.$string; return $new_string; } ?> That would return your value. Using this in another function would go like: <?php function echo($text){ $i=1; $string=' cars'; $new_text1=$text; $new_text2=concat($i, $string); $new_text3=$new_text1.new_text2; return new_text3; } I hope this is what you meant. If not, please make this a bit more clear. Maybe give some examples of what you tried so far. |
Quote:
Thanks for the answer, but i need it in C++ here what i want to do Quote:
writ jpg take char* input |
Use a stringstream (you'll need to include sstream), e.g.
string s = "stuff"; int i = 1; stringstream ss; ss << s << i; cout << ss.str() << endl; You can get a C string (char array) from an object of type string by using the c_str() method, i.e. ss.str() returns a string and you can get the C string from that with ss.str().c_str(). Edit: there may be other (better?) ways to do it, but that's the way I've always done it.. |
Quote:
Thanks i used sprintf it is more convenient |
If this problem is solved, please put this thread on SOLVED.
|
All times are GMT -5. The time now is 01:38 PM. |