9

How to display a Variable in MessageBox c++ ?

string name = "stackoverflow";

MessageBox(hWnd, "name is: <string name here?>", "Msg title", MB_OK | MB_ICONQUESTION);

I want to show it in the following way (#1):

"name is: stackoverflow"

and this?

int id = '3';

MessageBox(hWnd, "id is: <int id here?>", "Msg title", MB_OK | MB_ICONQUESTION);

and I want to show it in the following way (#2):

id is: 3

how to do this with c++ ?

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
a7md0
  • 429
  • 2
  • 10
  • 20

5 Answers5

10

Create a temporary buffer to store your string in and use sprintf, change the formatting depending on your variable type. For your first example the following should work:

 char buff[100];
 string name = "stackoverflow";
 sprintf_s(buff, "name is:%s", name.c_str());
 cout << buff;

Then call message box with buff as the string argument

MessageBox(hWnd, buff, "Msg title", MB_OK | MB_ICONQUESTION);

for an int change to:

int d = 3;
sprintf_s(buff, "name is:%d",d);
Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
Gibby
  • 458
  • 1
  • 4
  • 14
  • 1
    I've change sprintf to sprintf_s, and code working correctly **Thank you very much** – a7md0 Feb 07 '14 at 07:00
  • 1
    to show appreciation accept the answer by clicking the green check below the number. – Gibby Feb 07 '14 at 07:05
  • 1
    @Gaspa79 Erm you're not supposed to include winuser.h directly. Per the docs, [`#include ` instead](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox). – Asteroids With Wings Jan 26 '21 at 13:22
3

This can be done with a macro

#define MSGBOX(x) \
{ \
   std::ostringstream oss; \
   oss << x; \
   MessageBox(oss.str().c_str(), "Msg Title", MB_OK | MB_ICONQUESTION); \
}

To use

string x = "fred";
int d = 3;
MSGBOX("In its simplest form");
MSGBOX("String x is " << x);
MSGBOX("Number value is " << d);

Alternatively, you can use varargs (the old fashioned way: not the C++11 way which I haven't got the hang of yet)

void MsgBox(const char* str, ...)
{
    va_list vl;
    va_start(vl, str);
    char buff[1024];  // May need to be bigger
    vsprintf(buff, str, vl);
    MessageBox(buff, "MsgTitle", MB_OK | MB_ICONQUESTION);
}
string x = "fred";
int d = 3;
MsgBox("In its simplest form");
MsgBox("String x is %s", x.c_str());
MsgBox("Number value is %d", d);
cup
  • 7,589
  • 4
  • 19
  • 42
1

This is the only one that worked for me:

std::string myString = "x = ";
int width = 1024;
myString += std::to_string(width);

LPWSTR ws = new wchar_t[myString.size() + 1];
copy(myString.begin(), myString.end(), ws);
ws[myString.size()] = 0; // zero at the end

MessageBox(NULL, ws, L"Windows Tutorial", MB_ICONEXCLAMATION | MB_OK);
Gilles Walther
  • 126
  • 1
  • 7
1

It's not good to see people still messing with buffers. That was unnecessary in 1998, and definitely today.

std::string name = "stackoverflow";

MessageBox(hWnd, ("name is: "+name).c_str(), "Msg title", MB_OK | MB_ICONQUESTION);

If you're using Unicode (which makes sense in the 21st century)

std::wstring name = L"stackoverflow";

MessageBox(hWnd, (L"name is: "+name).c_str(), L"Msg title", MB_OK | MB_ICONQUESTION);
MSalters
  • 173,980
  • 10
  • 155
  • 350
0

Answer to your question:

string name = 'stackoverflow';

MessageBox("name is: "+name , "Msg title", MB_OK | MB_ICONQUESTION);

do in same way for others.

user3148898
  • 135
  • 2
  • 9
  • 1
    i try this `std::string mynm = "stackoverflow"; MessageBox(hWnd, L"name is: "+mynm, L"Msg title", MB_OK | MB_ICONQUESTION);` but the error is 1 **IntelliSense: no operator "+" matches these operands operand types are: const wchar_t [10] + std::string** – a7md0 Feb 07 '14 at 06:53
  • 1
    remove L from statement – user3148898 Feb 07 '14 at 06:56
  • 3
    This is flat-out wrong. The + operator does not concatenate string literals in C++; it performs pointer arithmetic. – ApproachingDarknessFish Feb 07 '14 at 07:10