1

I have been working in Java since I started programming and decided to learn c++. What I wrote in Java looked like this:

showMessage("Hello world" + randomNumber);

And it showed text + integer or float or whatever. But it wont work in c++. Error message by xCode: Invalid operands to binary expression ('const char *' and 'float')

Cheers!

AndroidXTr3meN
  • 399
  • 2
  • 9
  • 19

9 Answers9

2

You can do a sprintf according to Anton, or to be more c++:

std::stringstream ss;
ss << "Hello, world " << randomNumber;
showmessage(ss.str());

(there's nothing wrong with sprintf, especially if you use snprintf instead).

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
1
    ostringstream os;
    os<<"HelloWorld"<<randomnumber;
    string s;
    s = os.str();

string s now contains the string you want as a string object.

user93353
  • 13,733
  • 8
  • 60
  • 122
1

Also you can use boost::lexical_cast to cast numbers into strings which is fastest method in most cases:

showMessage("Hello world" + boost::lexical_cast<std::string>(randomNumber));

showMessage declaration is

void showMessage(cosnt std::string& message)
Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44
1

Consider adding a new function that is able to convert several types to std::string:

template<typename ty>
string to_str(ty t)
{
   stringstream ss; ss << t;
   return ss.str();
}

Usage:

"Hello World " + to_str(123)
  • @MichaelKrelin-hacker It won't fail. Concatenating `char*` with `string` is overloaded already in `` –  Sep 29 '12 at 10:23
  • I thought it's only for `string` being on the left. Maybe I've had this problem with old incomplete implementations, not sure. – Michael Krelin - hacker Sep 29 '12 at 10:25
  • Even through. It won't fail because it will be assigned to a string `string str = "hello World " + to_str(123);` –  Sep 29 '12 at 10:30
0

Define a class S. Then write

showMessage( S() << "Hello world" << randomNumber );

I've coded up the S class too many times for SO, and it's a good exercise to create it, hence, not providing the source code.

Note that you can reasonably call it StringWriter or something like that, and then just use a typedef for more concise code in function calls.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

I am not sure if c-style answer is fine, but I have already answer it here in a cocos2d-x question.

Trying to set up a CCLabelTTF with an integer as part of it's string in Cocos2d-X C++

Community
  • 1
  • 1
m.ding
  • 3,172
  • 19
  • 27
0

With C++11:

showMessage("Hello world" + std::to_string(randomNumber));
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
-1

you should print into the char* instead.

You could do something like

char* tempBuffer = new char[256];

sprintf_s(tempBuffer, 256, "Hello world %d", randomNumber);

showMessage(tempBuffer);
SinisterMJ
  • 3,425
  • 2
  • 33
  • 53
-1

In C++ the standard way to concatenate strings and primitives is to use stringstream. Which fulfils the same functionality (and a little bit more) as StringBuilder in Java (of course its API differs). However, if you are comfortable using cout then you should be fine.

eg.

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main () {
  stringstream ss;
  ss << "Some string - " << 124; // build string    
  string str = ss.str(); // extract string

  cout << str << endl;
  return 0;
}

Quick reference for stringstream http://www.cplusplus.com/reference/iostream/stringstream/stringstream/

Dunes
  • 37,291
  • 7
  • 81
  • 97