Hey,
I have this line to show a message box:
MessageBox(NULL, size, "Size", MB_OK);
size is a parameter (float) passed into a function.
How can I get a float variable to show in a message box.
it gives me an error.
Thanks,
Printable View
Hey,
I have this line to show a message box:
MessageBox(NULL, size, "Size", MB_OK);
size is a parameter (float) passed into a function.
How can I get a float variable to show in a message box.
it gives me an error.
Thanks,
you will need to convert your float to a char*.
there are several ways to do this:
C:
C++Code:#include <stdio.h>
// this code doesn't check for overflows. could be bad.
char szBuffer[32];
// copy the float into the buffer as astring
sprintf(szBuffer, "%f", size);
MessageBox(NULL, szBuffer, "Size", MB_OK);
clever boost library usage :PCode:#include <sstream>
// this code will not overflow.
std::stringstream sBuffer;
// insert the float into the buffer
sBuffer << size;
// .str() returns a string, .c_str() returns a const char*
MessageBox(NULL, szBuffer.str().c_str(), "Size", MB_OK);
www.boost.org
Code:#include <boost/lexical_cast.hpp>
#include <string>
MessageBox(NULL, boost::lexical_cast<std::string>(size).c_str(), "size", MB_OK);