1

Hi everyone I have the following line:

string* playerInfo = "Name: " + firstName + " " + lastName + "\n" +
                        "Number: " + playerNumber + "\n" +
                        "Points: " + pointTotal + "\n";

where firstName, lastName, playernumber, pointTotal are all string pointers.

How can I put them all together into another string pointer? The compiler complains about my line of code.

Sorry, I'm not good with C++ I come from a Java background.

  • 1
    "Sorry, I'm not good with C++ I come from a Java background." That isn't really relevant, though, because you should be learning C++ from the ground up anyway. Check out a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and forget you know Java; treat similarities as coincidences. – GManNickG Mar 30 '11 at 02:33
  • Not clear whether the `string pointers` C Strings or `std::string`. – Keith Mar 30 '11 at 02:35

6 Answers6

8

Use less pointers. That would have worked if all your variables were just strings. But since you say you have pointers:

string playerInfo = "Name: " + *firstName + " " + *lastName + "\n" +
                    "Number: " + *playerNumber + "\n" +
                    "Points: " + *pointTotal + "\n";

One of the first habits you should break when moving from Java to C++ is creating all your objects with new. In C++, new is not for creating objects, it's for memory allocation. Any time you can use a local variable instead of dynamic allocation, you should. And when you can't, try to let some library-provided object like std::vector take care of the allocation and deallocation for you.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
2

For putting a bunch of pieces together like this, I'd use a stringstream, something like this:

std::ostringstream buffer;
buffer << "Name: " << firstName << " " << lastName << "\n"
       << "Number: " << playerNumber << "\n"
       << "Points: " << pointTotal << "\n";

std::string PlayerInfo = buffer.str();
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

Java background is the problem.

You can't do this in C++. Pointers point to places in memory. They're in separate locations, so you can't just concatenate them.

Why are you using string pointers? You may be confused between char * pointers and string which is in the STL.

You probably just want to use strings, without pointers. You can do it like this:

string str;
str.append("Name: ");
str.append(firstname);
// ...

You can also use +=.

string str;
str += " ";
str += lastname;

But this is confusing, because you CANNOT do:

string str;
str += " " + " Name: "; // WRONG!

But you can do:

string str;
string str2;
str = "Name: " + str2; // OK

So I just avoid the operator + and use .append.


usul
  • 784
  • 5
  • 16
  • 2
    `std::string` overloads `operator +`, so you CAN just concatenate them. As long as at least one argument is a true `string` object, that is. – Ben Voigt Mar 30 '11 at 02:32
  • Are you sure? I think it is only +=... (edit nevermind, tested it, it works) – usul Mar 30 '11 at 02:33
  • 1
    @bo: Yes, he's right. A common error might be: `const char* cstr = "asd"; std::string s = cstr + "123";`. Here, this operator overloading doesn't come into play because neither `cstr` or `"123"` are `std::string`'s. – GManNickG Mar 30 '11 at 02:35
1

You probably want just:

std::string playerInfo = std::string("Name: ") + firstName + " " + lastName + "\n" +
                "Number: " + playerNumber + "\n" +
                "Points: " + pointTotal + "\n";

Putting "Name" into a std::string then creates a series of operator+() calls that in turn produce the concatenation.

You probably do not really need playerInfo to be on the heap, but if you do, you can have:

std::string* pOnHeap = new std::string(playerInfo);
Keith
  • 6,756
  • 19
  • 23
0

like Mr. Coffin´s answer using "stringstream":

#include <iostream>
#include <sstream>
using namespace std;

stringstream buffer;     
buffer << "Name: " << firstName << " " << lastName << "\n"
       << "Number: " << playerNumber << "\n"
       << "Points: " << pointTotal << "\n";

cout << buffer.str() << endl;
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

As you asked to solve your problem by using pointers here is an elementar school code solution:

int StringLength(const char * s){
int l = 0;
while (*s++) l++;
return l;
}

char *StrCat(const char * str1, const char *str2){
  int len1 = StringLength(str1);
  int len2 = StringLength(str2);
  int totLen = len1 + len2 + 1;

  char * str12 = (char *)malloc((totLen)*sizeof(char));
  memset(str12, '\0', totLen);

  for (int i = 0; i < len1; i++)
    *(str12 + i) = *(str1 + i);
  for (int i = 0; i < len2; i++)
    *(str12 + i + len1) = *(str2 + i);

  return str12;
}

int main(int argc, char *argv[]){
  char * S1= "ABCDE";
  char * S2= "FGH";

  char *S12 = NULL;
  S12 = StrCat(S1, S2);
  cout << "S12= "<< S12 << endl; // ABCDEFGH
}
AB13
  • 1