Jump to content
  • Advertisement
Sign in to follow this  
zel__

displaying variables in messagebox

This topic is 4352 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Try using char* foo = "a";

or use sprintf on a char array.

char buff[255];
int a = 23;

sprintf(buff, "a = %i", a);

// Message box code

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!