How do I put an int on the end of a C++ string object?

Started by
16 comments, last by Zahlman 18 years, 10 months ago
#define flag_BufferSize 100void main(){   /* Declare the memory pointer. */   char *strString1;   char *strString2;   int intNumber=5;   /* Create the memory. */   strString1 = calloc(flag_BufferSize , sizeof(char));   strString2 = calloc(flag_BufferSize , sizeof(char));   /* Place some stuff in each. */   strcpy(strString1, "Hello");   strcpy(strString2, " world.");   /* Combine em. */   strcat(strString1, strString2);   /* Print em. */   printf(strString1);   /* Append a filename(#intNumber) onto strString2. */   sprintf(strString2, "CPP2Abstract%d", intNumber);   /* Reprint em~*/   printf(strString2);   /* Free em */   free(strString1);   free(strString2);}


Untested, of course~ xD
Advertisement
OK, I've cleverly written my method with the help of you lot of course:

void Message::Add(int line, char* text1, char* text2, char* text3)
{
std::string stext1=text1;
std::string stext2=text2;
std::string stext2=text3;

std::ostringstream buffer;
buffer << text1 << text2 << text3;
m_sLine[line]=buffer.str();
}


You send it three C style strings or string literals and it joins them together and stores it in one of many C++ strings.

One problem though. I want you to be able to send it one, two or three strings, rather than having to send it three.

I know I can do it by overloading the function, ie creating three methods:

void Add(int line, char* text);
void Add(int line, char* text1, char* text2);
void Add(int line, char* text1, char* text2, char* text3="");

Works just fine but seems a little messy! Is there a simpler way? Something to do with default arugments mebbe?

Quote:Original post by joebarnslondon
Works just fine but seems a little messy! Is there a simpler way? Something to do with default arugments mebbe?

If you want to do any kind of operations (like add them in this case) on strings you could better use a string class such as std::string.
Quote:Original post by joebarnslondon
OK, I've cleverly written my method with the help of you lot of course:

void Message::Add(int line, char* text1, char* text2, char* text3)
{
std::string stext1=text1;
std::string stext2=text2;
std::string stext2=text3;

std::ostringstream buffer;
buffer << text1 << text2 << text3;
m_sLine[line]=buffer.str();
}


//....

Works just fine but seems a little messy! Is there a simpler way? Something to do with default arugments mebbe?


I don't see the point in any of this as the string streams work with C-style strings directly aswell, if you didn't notice in my previous example i was using string literals which are immutable C-style strings.
Good point. I'm such a dummy. Got the hang of it now though.

Thanks all!
1) As mentioned, string streams will append the data pointed at by a char* just fine. However...

2) Strings can be added to each other, or to a char* literal, with the string operator+ - so if all portions are textual, there is no real problem.

string("foo") + string("bar"); // okstring("foo") + "bar"; // ok"foo" + string("bar"); // ok"foo" + "bar"; // not ok! Trying to add two pointer values together


3) boost::lexical_cast is probably the most convenient and reliable way of converting data to a string.

Thus our function could just be like:

void Message::Add(int line, const std::string& data) {  m_sLine[line] = data;}// Used like:Message m;m.Add(0, "foo"); // implicit conversion to std::string, IIRCm.Add(1, string("foo") + "bar"); // the addition is processed before the// function call, so the explicit string creation is neededm.Add(2, string("file") + boost::lexical_cast<std::string>(whichFile) + ".dat");


Nice and simple :)
A guy asks how to add strings, do you really think hes going to be able to get boost to work? Id think that stringstream is the easiest most robust choice vs the C routines.
Quote:Original post by EvilCrap
A guy asks how to add strings, do you really think hes going to be able to get boost to work? Id think that stringstream is the easiest most robust choice vs the C routines.


The Boost solution is built upon stringstreams anyway; it just does some extra "never would have thought of that" stuff to handle some special cases, and provides meaningful error handling for bad usage (via the bad_lexical_cast - IIRC - exception). Anyway, skill with the language is nothing to do with skill in getting someone else's thing to compile, and the boost::lexical_cast provides a nice simple interface :)

Of course, a forum search (or perhaps a Google search restricted to this site) will turn up several versions of a "basic hand-rolled lexical_cast".

This topic is closed to new replies.

Advertisement