Sign in to follow this  
zel__

displaying variables in messagebox

Recommended Posts

Hi :) I'm trying to output a char and integer to a message box using win32. I swear I used to be able to just be able to use: char foo = 'a'; MessageBox(NULL, foo, "Test", MB_OK); error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'char' to 'LPCSTR' maybe not, maybe I'm an idiot :P What's the easiest way of displaying chars and ints in a messageBox using win32? Thanks!!

Share this post


Link to post
Share on other sites
You need to have a char *. My favorite way of printing chars and ints is to use sprintf. Such as:


char foo = 'a';
int bar=0x1337c0de;
char MessageBoxBuff[256];
sprintf(MessageBoxBuff, "%c %d", foo, bar);
MessageBox(NULL, foo, "Test", MB_OK);




Share this post


Link to post
Share on other sites
Using std::stringstream is type-safe so it is better to use it. You can do it like this:

#include <string>
#include <sstream>
#include <windows.h>

#pragma comment( lib, "user32.lib" )

int main ()
{
std::ostringstream stream;
stream << 'c' << " " << 0.5f << " " << 4 << std::endl;
::MessageBox(0, stream.str().c_str(), "Test", MB_OK);

stream.str("");
int someVar = 10;
stream << "My Integer: " << someVar << std::endl << "My Float: " << 1.18f << std::endl;
::MessageBox(0, stream.str().c_str(), "Test", MB_OK);

return 0;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this