Skip to main content
GameDev.net gamedev.net
🔒 Locked

displaying variables in messagebox

Started by zel__ Aug 17, 2006 at 11:52 PM 3 replies 600+ views
Original Post
zel__
zel__
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!!
ordered_disorder
ordered_disorder
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);
LorenTapia
LorenTapia
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
=Loren Tapia=
Paulius Maruska
Paulius Maruska
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;}
zel__
zel__
wow, you guys are good :D thanks heaps guys!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.