LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Concatenating an integer to a string (https://www.linuxquestions.org/questions/programming-9/concatenating-an-integer-to-a-string-838746/)

mohabic 10-18-2010 01:15 AM

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

Mark1986 10-18-2010 01:26 AM

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.

mohabic 10-18-2010 01:40 AM

Quote:

Originally Posted by Mark1986 (Post 4130924)
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.



Thanks for the answer, but i need it in C++
here what i want to do

Quote:

for (int i=0;i<3;i++) {

temp1.WriteJpg(strcat(i, "binomial"));
temp2.WriteJpg(strcat(i, "median"));
temp3.WriteJpg(strcat(i, "gaussian"));
temp4.WriteJpg(strcat(i, "outlier"));
temp5.WriteJpg(strcat(i, "knn"));

}
but it doesnt work !!!!
writ jpg take char* input

Nylex 10-18-2010 01:42 AM

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..

mohabic 10-18-2010 03:54 AM

Quote:

Originally Posted by Nylex (Post 4130935)
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..


Thanks
i used sprintf it is more convenient

Mark1986 10-19-2010 01:34 AM

If this problem is solved, please put this thread on SOLVED.


All times are GMT -5. The time now is 01:38 PM.