It seems like a really small issue, but I'm fairly new to c++ and I'm finding it surprisingly difficult to this small task. I have a function called draw_text(const char* text) which outputs the variable "text" in my openGL window. When I call the function on, let's say draw_text("example"), then it draws the text example into my openGL window perfectly. But, I've been trying to give the function the input "score: "+score. Where "score: " is a string and is printed as shown, and score is an integer variable containing the current score in the game. I know that this implementation works fine in java, but in c++ it prints out random text, which I can't find anywhere in my code. e.g. when I first run my programme it prints out "r_text.png" in the position where "score: "+score should be printing, and then the text keep changing to another random word.
I've found several methods for converting integers to strings, but none which I can find useful for my case. I've tried several stream methods, but they only print out text in the console, they don't help storing a variable with the concatenation of strings.
I've tried using the sprintf() method
char stringResult[21]; // enough to hold all numbers up to 64-bits
sprintf(stringResult, "%d", score);
std::string result = "Score: " + stringResult;
but it gives compile time errors saying invalid operands of types 'const char [8]' and 'char [21]' to binary 'perator+'
I've tried "Score: "+(char)score, but this started to print out random text just as my first attempt, does anyone know why it's printing out this text rather than my input.
I've tried the itoa() method, but it's not recognised in c++
I've tried using the strcat() method as
char str[21];
strcpy (str,"Score: ");
strcat (str,(char)score);
but this gives an erro in my console saying invalid conversion from 'char' to 'const char*'
The methods string() and to_string aren't recognised in my version of C++, even though I have included the library.
Is there a very simple way of doing this in C++ that I just can't find anywhere, or is the language that bad that trying to do one of the most simplest tasks is this frustrating.
My method for draw_text() is given below
void CTetrisGame::draw_text(const char* text)
{
size_t len = strlen(text);
for (size_t i=0;i<len;i++)
glutStrokeCharacter(GLUT_STROKE_ROMAN, text[i]);
}