(SDL) Putting an integer in a char*?

EDIT: Solved relatively quickly. New question in post 3.



Hi.

I'm very new to SDL and I'm currently fooling around with it. I want to set the window caption to a string. However, I'm pretty dum so I have no clue how to do this correctly lol

For my string I put

std::string title = "Ball Bounce";

And for the Caption I put

SDL_WM_SetCaption(title, NULL );

I get the error
"cannot convert `std::string' to `const char*' for argument `1' to `void SDL_WM_SetCaption(const char*, const char*)' "

I have included <string> and "sdl.h".

Uh... what should I put in the SDL_WM_SetCaption thing instead? Or is there something I just missed?
Last edited on
Well, you could try this:

char* title = "Ball Bounce";

instead of using std::string title = "Ball Bounce";

This should work :D

Or, you could save yourself the time and just do something like this:

SDL_WM_SetCaption("Ball Bounce", NULL);
Last edited on
Oh yea... derp!!

I wanted to put it in a value so I could change the string on the fly with a number that goes up constantly beside the title.

But now I got the problem of making a string with a variable in it... lol

char* title = "Ball Bounce - " && ballcount && " Balls";

Gives me
"cannot convert `bool' to `char*' in initialization "

Which is understandable. I forget how to include an integer in a string of sorts (as it is it thinks it's a boolean because it's 1). Can anyone refresh me?
Last edited on
Last edited on
This answers your first question, which makes your second question easy.

For my string I put

 
std::string title = "Ball Bounce";

And for the Caption I put
1
2
3
4
5
//This was your error...
SDL_WM_SetCaption(title, NULL );

//This fixes your error...
SDL_WM_SetCaption(title.c_str(), NULL );


Now keep using the std::string and build your title much easier than using the char *. Use a stringstream to concat the numbers onto the string.

Last edited on
@clanmjc
Now keep using the std::string and build your title much easier than using the char *.


O.o What do you mean easier?
Poor choice of words, we use boost, so string manipulations are very trivial with lexical cast especially when concat like the OP is doing.
Ah thanks!

clanmjc's suggestion did the trick. Program's working perfectly now!

Awesome!
Topic archived. No new replies allowed.