LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-18-2010, 01:15 AM   #1
mohabic
LQ Newbie
 
Registered: Nov 2009
Posts: 26

Rep: Reputation: 15
Question 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
 
Old 10-18-2010, 01:26 AM   #2
Mark1986
Member
 
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Rep: Reputation: 11
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.
 
Old 10-18-2010, 01:40 AM   #3
mohabic
LQ Newbie
 
Registered: Nov 2009
Posts: 26

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mark1986 View Post
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
 
Old 10-18-2010, 01:42 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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..

Last edited by Nylex; 10-18-2010 at 01:58 AM.
 
Old 10-18-2010, 03:54 AM   #5
mohabic
LQ Newbie
 
Registered: Nov 2009
Posts: 26

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Nylex View Post
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
 
Old 10-19-2010, 01:34 AM   #6
Mark1986
Member
 
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Rep: Reputation: 11
If this problem is solved, please put this thread on SOLVED.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to convert string to integer back to string C++ LazerPhreak Programming 1 03-13-2010 07:32 PM
c convert integer to string? kpachopoulos Programming 5 08-23-2006 11:57 AM
Problem concatenating a string in perl Singist Programming 4 03-22-2006 06:15 PM
C how to convert an integer to string? totti10 Programming 10 11-10-2005 05:49 AM
Integer To a String in C Bean101 Programming 2 05-27-2004 04:46 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration