Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display an int variable in a MessageBox

Tags:

c++

winapi

I am working on an old app written in Visual C++ 6.0. I am trying to display an int variable in a MessageBox for debugging reasons. Here is my code, I thought this would be a simple process, but I am just learning C++. The two lines that are commented I have tried as well with similar errors. Below is the error I am getting.

int index1 = 1;
char test1 = index1;
// char var1[] = index1;
// char *varGo1 = index1;
MessageBox(NULL, test1, "testx", MB_OK);

error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'char' to 'const char *'

like image 257
user3076025 Avatar asked Dec 06 '13 21:12

user3076025


2 Answers

Why bother with C-style strings if you tagged C++?

Although Mark Ransom provided MFC solution (which is perfectly valid), here is a Standard C++ one:

int index1 = 1;
std::string test1 = std::to_string(index1);
MessageBoxA(NULL, test1.c_str(), "testx", MB_OK);

References:

  • std::to_string();
  • Arrays are evil

Use boost::format for more sophisticated formatting.

like image 79
Ivan Aksamentov - Drop Avatar answered Sep 20 '22 14:09

Ivan Aksamentov - Drop


CString str1;
str1.Format(_T("%d"), index1);
MessageBox(NULL, str1, "testx", MB_OK);

CString's Format works just like printf to populate the string with the parameter list.

like image 23
Mark Ransom Avatar answered Sep 18 '22 14:09

Mark Ransom