• Advertisement
Sign in to follow this  

Converting ints to char*s (Has to do with SDL_ttf)

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Last week I set up and got the basics of SDL_ttf down. Now I'm trying to get the program to print out the mouse coordinates on the screen. The only problem is that TTF_RenderText can only take a char* as the parameter for the text. Simply declaring (char*) before the int variable of the mouse coordinates doesn't cut it becuase the program crashes when I do so. Can anybody help me out here?

Share this post


Link to post
Share on other sites
Advertisement
More specifically, something along the lines of

char buffer[256];

sprintf(buffer, "%i", my_integer);


snprintf() provides a bit more security since it won't overflow the buffer if the input is too long

Share this post


Link to post
Share on other sites
*smacks self* Duh! How could I forget about sprintf!? I used that in my old font engine. Derrrrr, stupid me. Thanks guys. :thumbs up:

Share this post


Link to post
Share on other sites
If you happen to come back and look, heres a solution I came up with :

name this convert.h

#ifndef CONVERT_H
#define CONVERT_H

#include <string>
#include <sstream>
using namespace std;

string convert(int _in);
string convert(float _in);
string convert(bool _in);

#endif


name this convert.cpp

#include <string>
#include <sstream>
using namespace std;
#include "SDL.h"


string convert(int _in)
{
string temp;
stringstream numb;

numb << _in;
numb >> temp;
return (temp);
}
string convert(float _in)
{
string temp;
stringstream numb;

numb << _in;
numb >> temp;
return (temp);
}
string convert(bool _in)
{
if (_in)
return ("True");
else return ("False");

}



then #include "convert.h" where you need it use like

string temp;
temp = convert(10);
then use temp.c_str();
you can override convert with any type you need to add.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement