Advertisement

Converting float to strings

Started by May 06, 2000 06:57 AM
6 comments, last by skrwX 24 years, 7 months ago
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..)
How about sprintf()?
Advertisement
wsprintf(), a Win32 API, may also help you

Kwanji
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!
char sz[64];
TextOut(hDC, 10,10, sz, wsprintf(sz, "%i", (int)(fps*100)));


Too much trouble when in Visual Basic, you could do a cstr().
Advertisement
THX to all my helpers!

I finally got it!

skrwX
a note, wsprintf() doesn''t work with floating point numbers (according to MSDN anyway

if you want floating point, you''d need to use sprintf()

adamm@san.rr.com
adamm@san.rr.com

This topic is closed to new replies.

Advertisement