Archived

This topic is now archived and is closed to further replies.

bslayerw

Convert float to char *

Recommended Posts

I am new to C/C++ and have a silly question: how do I convert a float type to a char * (pointer to char) type? All I want to do is display a damn floating point number in a message box (which takes a char * as the message parameter). Edited by - bslayerw on November 15, 2001 7:45:42 PM

Share this post


Link to post
Share on other sites
Don''t know if this is the generally accepted way of doing it, but it''s the only thing I can think of right now:
  
// assuming you have a float f you want to print

char msg[64];
sprintf (msg, "The value of f: %f", f);
MessageBox (NULL, msg, "", MB_OK);


-Neophyte


- Death awaits you all with nasty, big, pointy teeth. -

Share this post


Link to post
Share on other sites
"msdn.microsoft.com".

all ANSI C functions are described in detail, as well as all the Win32 functions. it even includes differences between functions under different versions of Windows. (i.e. Win95/98/ME/NT/2000/XP).

just search under the function name.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
Sooner or later, this 64 bytes buffer will not be enough. This method is more general, type safe, and it work exactly like cout :

#include
using namespace std;

ostringstream out;
out << "The value of f: " << f;
MessageBox(NULL, out.str().c_str(), NULL, MB_OK);

Share this post


Link to post
Share on other sites
actually BUFSIZE = 512 in "stdio.h".

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
Ah cool, a string buffer. That will probably work nicely. Thanks for the help, coming from the Java world where everything is in plain sight (io package etc) makes it a little awkward to dig around and find library functions. Oh well, it's fun fun fun. I see you call str() then c_str() on the ostringstream is that to flush it?

Actually nevermind I just looked it up on msdn, c_str return a "immutable" string with a null terminator and str return a "basic_string" using the STL.

Cool, couple more years and I should know this stuff.

Edited by - bslayerw on November 15, 2001 9:12:48 PM

Share this post


Link to post
Share on other sites
Cool, I''ll check it out. I actually have a "C++ Primer Plus", and it''s just that, a primer. I understand OO, UML, Patterns etc. I have been doing OO for a while. I just need to figure out how to apply my OO and patterns skills to C++.

Thanks again for all the help.

Share this post


Link to post
Share on other sites
i have stroustrup''s book, he doesn''t mention technicalities of STL much, i''m thinking i should get PJ Plauger''s book, but for now does anyone know where they use splay or red and black trees in some of the stl? i believe they do in sgi''s implementation

Share this post


Link to post
Share on other sites
If what you''re looking for is a reference-book you can''t go wrong with Herbert Schildt''s "C/C++ Programmer''s Reference".
It''s published by Osborne McGraw-Hill, ISBN: 0-07-212706-6

-Neophyte

- Death awaits you all with nasty, big, pointy teeth. -

Share this post


Link to post
Share on other sites