Equivalent of [NSString stringWithFormat: ] in Cocos2d-x ?

Ok, my question is perhaps more a pure C++ question than a Cocos2d-x question nevertheless as I just discovered CCMutableArray (much better than using C++ vectors , I ask the question )

What is the best way to “translate” the following Objective-C code to Cocos2d-X ?

[NSString stringWithFormat: @"my string = %@, my int = %d, my double = %f", myString, myInt, myDouble];

In other words, what is the easiest way to concatenate stings together ? Or strings with integers and doubles ?

Thanks

try searching for
do it like this

#include
using namespace std;
//put this on top

stringstream tempString;
int yourInt;
double yourDouble
tempString <<“yourString”<<yourInt<<yourDouble;

tempString.str() //Converts this into string;
tempString.str().c_str() //Converts this into const char*;

You can also solve it by this way:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

char fn[64];
int pageNum = 2;
sprintf(fn, “myImage%d.png”, pageNum);
CCSprite* pSprite = CCSprite::spriteWithFile(fn);

Hey I used this and it worked fine for me… Same Code I used in other function and its giving bad_excess…
Its Damn frustrating. :frowning: I’ve attached a screenshot. Please tell me wt wrong m doing… ???

You’re attempting to write data into a pointer that hasn’t been pointed to any valid memory. You either need to declare your char *scoreadd as char scoreadd[25] or however big it needs to be, or you need to somehow dynamically determine the size and do something like char *scoreadd = (char*)malloc(sizeof(variable));

Thanks buddy … Problem is solved by just allocating it as
Char *score = new char;
:slight_smile: