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]);
}
Richard Williams
  • 291
  • 2
  • 11
Andrew Brick
  • 276
  • 2
  • 4
  • 18
  • std::string result = "Score: " + std::string(stringResult); – user3159253 Dec 16 '14 at 02:44
  • But, fellow, why don't you simply use C++ input methods everywhere? Why do you need `sprintf()` at all? – user3159253 Dec 16 '14 at 02:46
  • possible duplicate of [C++ concatenate string and int](http://stackoverflow.com/questions/191757/c-concatenate-string-and-int) – Reto Koradi Dec 16 '14 at 02:47
  • http://en.cppreference.com/w/cpp/string/basic_string/to_string – user3159253 Dec 16 '14 at 02:47
  • @RetoKoradi no, not completely, but this basic C++ stuff always looks similar :) – user3159253 Dec 16 '14 at 02:48
  • @user3159253 According to the question, the user isn't using C++11, so to_string isn't available. – Richard Williams Dec 16 '14 at 02:48
  • Likely, he doesn't know what he's using :) – user3159253 Dec 16 '14 at 02:49
  • thank you, I'm no longer receiving any errors on the code which does the concatenating. However, now I'm receiving errors that I'm calling draw_text(result), but draw_text can only take input of type const char*. How would I now convert result to a const char*? – Andrew Brick Dec 16 '14 at 02:52
  • Certainly there're plenty of methods to convert a number to string, using `boost::lexical_cast<>()` like in the forementioned recipe, [std::ostringstream](http://en.cppreference.com/w/cpp/io/basic_ostringstream/str) which is used by boost::lexical_cast internally AFAIR... – user3159253 Dec 16 '14 at 02:53
  • Please read a decent C++ _book_ ! Otherwise you'd have to ask question on every second line of your code. – user3159253 Dec 16 '14 at 02:55
  • also @user3159253 what do you mean by using c++ methods everywhere? – Andrew Brick Dec 16 '14 at 02:55
  • `printf()` is a C function from those dark ages, when programmers had to write their programs on sheets, and then specially trained people typed those programs on ugly looking terminals. – user3159253 Dec 16 '14 at 02:58
  • I've read through several pdfs regarding the problem, but it's not helping with my specific problem, all I need to do now is convert the result to an appropriate type for the method draw_text. Or can you advise me on how I can change draw_text(const char* input) so that it can take input of type string, i.e draw_text(std::string input) – Andrew Brick Dec 16 '14 at 02:58
  • If a function takes `const char*` argument, then you can't simply pass `std::string`. `std::string` has `c_str()` method which returns "a C representation of std::string". – user3159253 Dec 16 '14 at 03:01
  • I tried running the function with draw_text(result.c_str()) and it works perfectly :D – Andrew Brick Dec 16 '14 at 03:11

2 Answers2

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

Community
  • 1
  • 1
Richard Williams
  • 291
  • 2
  • 11
  • 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. – Andrew Brick Dec 16 '14 at 03:06
  • 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??? – Andrew Brick Dec 16 '14 at 03:08
  • "example" isn't of type std::string. [This question](http://stackoverflow.com/questions/3535824/c-is-my-text-a-stdstring-a-char-or-a-c-string) should clear up your confusion. :) – Richard Williams Dec 16 '14 at 03:10
  • Thank you very much, the code works perfectly now :D – Andrew Brick Dec 16 '14 at 03:13
  • 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 – Andrew Brick Dec 16 '14 at 03:14
  • As mentioned in the question I linked, "example" would be const char*. The difference between std::string and const char* is well explained in [this question](http://stackoverflow.com/questions/801209/c-char-vs-stdstring) - I'd explain myself, but that does a far better job than I could. :) The main take-away point is that you should use std::string wherever you can. It's a lot more modern and easier to deal with. You can convert const char* to std::string with one of the [std::string constructors](http://www.cplusplus.com/reference/string/string/string/). – Richard Williams Dec 16 '14 at 03:19
  • Thank you Richard, just one last question, what size value should I use when I define, char stringResult[size] – Andrew Brick Dec 16 '14 at 03:24
  • That's one of the reasons to prefer using std::string - you don't have to worry about that sort of thing. I'd really recommend converting your entire flow to use std::string and only calling string::c_str if/when you need to pass a const char* into a function. However, to answer your question, it should just be as many characters as you think you'll need. – Richard Williams Dec 16 '14 at 03:27
0

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

std::string result = "Score: " + std::to_string (score);
K_Finlay
  • 523
  • 3
  • 15