Results 1 to 2 of 2

Thread: Message Box

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    Message Box

    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,
    Don't anthropomorphize computers -- they hate it

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    you will need to convert your float to a char*.

    there are several ways to do this:

    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);
    C++
    Code:
    #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);
    clever boost library usage :P
    www.boost.org
    Code:
    #include <boost/lexical_cast.hpp>
    #include <string>
    
    MessageBox(NULL, boost::lexical_cast<std::string>(size).c_str(), "size", MB_OK);
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width