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++ ?
Answer to your question:
string name = 'stackoverflow';
MessageBox("name is: "+name , "Msg title", MB_OK | MB_ICONQUESTION);
do in same way for others.
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With