Search code examples
c++xcodecharcocos2d-x

C++ how to add more strings into a method


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!


Solution

  • 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).