Bit of a noob question this. I am using a library which has this function: int TextAdd(int x, int y, char *text, int font); I need to use this function to display a score. I have the variable int score and I would like to put it into that function in the style "Score: <variable>" at char *text , but I have no idea how to do this. Please can somebody show me how to convert int to char * and add letters to it?

Thanks :)
Mark

You have to format that char* before calling TextAdd(). There are several ways to format it, one way is to use std::stringstream from <sstream> header file, another way is to use sprintf().

#include <sstream>

int main()
{
   int score = 123;
   stringstream str;
   str << score;
   std::string s;
   s = "Score: ";
   s += str.str();
}

or

#include <cstdio>

int main()
{
   int score = 123;
   char text[80];
   sprintf(text,"Score: %d", score);
}
commented: just what I needed...thanks :) +8

Thanks for the reply - I used the sprintf method.
Cheers :)
(solved + rep)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.