1

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]);
}
15
  • std::string result = "Score: " + std::string(stringResult); Commented Dec 16, 2014 at 2:44
  • But, fellow, why don't you simply use C++ input methods everywhere? Why do you need sprintf() at all? Commented Dec 16, 2014 at 2:46
  • possible duplicate of C++ concatenate string and int Commented Dec 16, 2014 at 2:47
  • en.cppreference.com/w/cpp/string/basic_string/to_string Commented Dec 16, 2014 at 2:47
  • @RetoKoradi no, not completely, but this basic C++ stuff always looks similar :) Commented Dec 16, 2014 at 2:48

2 Answers 2

1

Ideally, if you were using C++11, you could use std::to_string, which would solve your problem straight away.

However, you could also convert the value to a string using stringstreams as such:

#include <sstream>

const std:string ConvertIntToString(const int input_int)
{
  std::stringstream ss;
  ss << input;
  return ss.str();
}

You should then just be able to concatenate this with your "Score: " string.

There are several more methods for converting an int to a string in answer to this question.

Edit: In answer to your question in the comments, you can convert from a string to const char* by calling string::c_str().

Sign up to request clarification or add additional context in comments.

8 Comments

I broke down my problem into 2 steps, converting score to a string, concatenating that string with "Score: ". I've tried your method before and it works fine, but I still don't know how I would concatenate the string with "Score: " afterwards. I found a way to concatenate and convert to strings effectively from another users comments, however my draw_text() doesn't accept the result as input.
The draw_text(const char* input) doesn't accept the concatenated string as input, because the resultant string is of type std::string, however draw_text can only take input of type const char*. But I don't understand how draw_text("example") works fine when "example" is also of type std::string???
"example" isn't of type std::string. This question should clear up your confusion. :)
Thank you very much, the code works perfectly now :D
just out of curiosity is the object "example" of type std::string or of type const char* and what's the difference between the two. Because apparently even the printf() method only takes input of type const char*. Also is there a way to convert from const char* to std::string
|
0

If you have a C++11 compiler, you could try std::to_string.

std::string result = "Score: " + std::to_string (score);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.