Converting float to strings
Hi everybody!
I want a fps-counter in my game(for testing purposes) and I want the fps to appear on my screen. I use the TextOut function but cannot convert the fps(in float) to a string that TextOut can work with!
How can I convert this sucking fps?
(I don''t use the MFC! So no CString-blabla..)
While I usually use sprintf when i only have 1 little variable to convert to a string, if you are using C++, you may want to look into the stringstream classes (or strstream in some implementations). These classes do all of the stuff you are used to when sending variables out to a file or cin. code like this:
// START CODE
struct Person
{
int age;
float height;
};
Person me;
cout << "Age: " << me.Age << "Height: " << me.Height << endl;
// END CODE
would automatically convert the int and float for you ... right?
well that''s what stringstreams do.
you do this instead of above:
//CODE
ostringstream buff;
buff << myFloat << myInt << 43.76 << "YEAH!" << endl;
and then buff is set to a line of text just like a text file or console would be.
you access the buffer with the function str(void);
like:
RenderFPSOnScreen(buff.str());
good luck!
// START CODE
struct Person
{
int age;
float height;
};
Person me;
cout << "Age: " << me.Age << "Height: " << me.Height << endl;
// END CODE
would automatically convert the int and float for you ... right?
well that''s what stringstreams do.
you do this instead of above:
//CODE
ostringstream buff;
buff << myFloat << myInt << 43.76 << "YEAH!" << endl;
and then buff is set to a line of text just like a text file or console would be.
you access the buffer with the function str(void);
like:
RenderFPSOnScreen(buff.str());
good luck!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement