2

Possible Duplicate:
C++ concatenate string and int

I am tryinging to make use many strings and ints to make a single string but I am getting the message: "error C2110: '+' : cannot add two pointers"

This is my code:

transactions[0] = "New Account Made, Customer ID: " + ID + ", Customer Name : " + firstName + " " + secondName + endl + ", Account ID: " + setID + ", Account Name: " + setName;

(note that ID and setID is an int)

Community
  • 1
  • 1
James Eaton
  • 287
  • 1
  • 2
  • 8

2 Answers2

2

Use a stringstream:

#include <sstream>

...
std::stringstream stm;
stm<<"New Account Made, Customer ID: "<<ID<<", Customer Name : "<<firstName<<" "<<secondName<<std::endl<<", Account ID: "<<setID<<", Account Name: "<<setName;

You can then access the resulting string with stm.str().

Andrei Tita
  • 1,226
  • 10
  • 13
1

You should use a string stream: write the string into it; then write the int. Finally, harvest the result through the str() method of the stream:

stringstream ss;
string hello("hello");
int world = 1234;
ss << hello << world << endl;
string res = ss.str();
cout << res << endl;

Here is a link to a demo on ideone.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523