Public Group

converting int to const char*

This topic is 3174 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

how would i convert an int to const char* i need to do this because im using the SDL_TTF extention library to render text. i have my to scores(int) and i have my two surfaces(SDL_Surface*) the
TTF_RenderText_Solid
function needs const char* EDIT: the error im getting is this:
main.cpp(143) : error C2664: 'TTF_RenderText_Solid' : cannot convert parameter 2 from 'int' to 'const char *'

with this code:
lscore_spr = TTF_RenderText_Solid(font, lscore, textColor);

Share on other sites
In C++, we use boost::lexical_cast, or std::stringstream:
#include <string>#include <boost/lexical_cast.hpp>// ...std::string text = boost::lexical_cast<std::string>(lscore);lscore_spr = TTF_RenderText_Solid(font, text.c_str(), textColor);

Or:
#include <string>#include <sstream>// ...std::stringstream stream;stream << lscore;std::string text = stream.str();lscore_spr = TTF_RenderText_Solid(font, text.c_str(), textColor);

Share on other sites
Thanks

But theres one thing:
i have to draw two different scores(pong game)

this is my code:
//render scores//leftstream << lscore;text = stream.str();lscore_spr = TTF_RenderText_Solid(font, text.c_str(), textColor);text.c_str() = "";//rightstream << rscore;text = stream.str();rscore_spr = TTF_RenderText_Solid(font, text.c_str(), textColor);text.c_str() = "";draww(40,40,lscore_spr,screen);draww(40,600,rscore_spr,screen);

it just prints one 0 after the other:
00000000000000000
the two score vars(lscore and rscore) are set to 0

i tryed to understand the problem more by using a log file to log what text.c_str() was every time i used it. It came up with something like this:
000000000000000000000000000000000000

so i thought that this:
text = stream.str();

so i tyed to set it to NULL after it got the string i needed to use
this is the error i got:
main.cpp(150) : error C2106: '=' : left operand must be l-value

Share on other sites
The simplest solution is to create a new stringstream every frame, and for each score variable. For example, make a function:
std::string toString(int number){    std::stringstream stream;    stream << number;    return stream.str();}SDL_Surface *renderNumber(TTF_Font *font, int number, SDL_Color colour){    return TTF_RenderText_Solid(font, toString(number).c_str(), colour);}// ...lscore_spr = renderNumber(font, lscore, textColor);rscore_spr = renderNumber(font, rscore, textColor);// ...

Share on other sites
thankyou, but...

the compiler is complaining about missing
;
in my classes now
EDIT: btw it want doing that before i tryed your code

Share on other sites
Quote:
 Original post by Lithso i thought that this:text = stream.str();was adding to it

There are two conceptual problems here.

1) This:

text.c_str() = "";

does nothing useful. The .c_str() member function returns a pointer, after all, so this is just taking the copy of the string's internal pointer and assigning it to point at an empty string. This has no effect on the text pointed at by the string.

This is deliberate: you are not supposed to be able to re-point the string's internal pointer. The string is responsible for keeping track of that pointer, not you. If you want to clear out the string, assign to the string.

2) But even then, assigning to the string would have no effect on the stream, because the string 'text' is, again, a copy of the stream's internal string object.

Rip-off's code gets around this by re-creating the stream from scratch every time (via calling the function). Notice that he doesn't reset the contents of anything afterward; there is no need to, because the stream is thrown away, and the string can also be thrown away once you've passed its .c_str() to the rendering function.

Quote:
 the compiler is complaining about missing ; in my classes now

That's because there's a ; missing somewhere. Error messages include line numbers, which should point you to the right part of the code. You're supposed to be able, by now, to read code and figure out where simple things like this have gone wrong. This kind of problem-solving is more basic than doing stuff like rendering text with SDL, so you should make sure you can do it yourself first.

Share on other sites
it said that im missing ; 's on this line:
SDL_Surface* LoadImg(blah blah blah);

its saying:
\func.cpp(10) : error C2143: syntax error : missing ';' before '*'

after some google searches i found out that theres proberly a missing ; somewhere in a different file, even though it said it was missing in a different file.

I found out that i was missing a ; in a header file

Thanks for explaining it to me

Share on other sites
This may be useful: itoa()

Share on other sites
Quote:
 This may be useful: itoa()
Is there any particular reason you would recommend itoa() over the previous suggestions, lexical_cast and stringstream? (Note that itoa() is non-standard, and may not even be available to the OP.)

Share on other sites
Hello Lith,

you can also use sprintf ( from <cstdio> ).

char buf[80];sprintf( buf, "%d", number );

1. 1
Rutin
24
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 46
• 41
• 23
• 13
• Forum Statistics

• Total Topics
631749
• Total Posts
3002039
×