ShowMessage包含文本和整数组件

时间:2012-09-24 04:14:23

标签: c++ string integer message

我无法显示由一些文本和整数

组成的消息

这是我的代码:

int integerNumberOfImportantAppointments = calCalendar.getNumberOfImportantAppointments();

if (integerNumberOfImportantAppointments > 0)
{
    ShowMessage("You have " + integerNumberOfImportantAppointments + " important appointments. Do you wish to view them?");
}

我收到以下错误:E2085指针添加无效

我能帮助你解决这个问题吗?

由于

3 个答案:

答案 0 :(得分:2)

不是使用sprintfitoa(或类似),而是使用std::istringstream可能会更好:

int iNumberOfImportantAppointments = calCalendar.getNumberOfImportantAppointments();

if (iNumberOfImportantAppointments > 0)
{
    std::istringstream istr;
    istr << "You have " << iNumberOfImportantAppointments << " important appointments. Do you wish to view them?";
    ShowMessage(istr.str().c_str());
}

PS。描述性变量/函数名称很好,但也有这样的太长名称。 :)

答案 1 :(得分:0)

关于这个:

int integerNumberOfImportantAppointments = calCalendar.getNumberOfImportantAppointments();
char buffer[30];
if (integerNumberOfImportantAppointments > 0)
{
itoa (integerNumberOfImportantAppointments,buffer,10);
    ShowMessage("You have " + buffer + " important appointments. Do you wish to view them?");
}

答案 2 :(得分:0)

试试这个:

int integerNumberOfImportantAppointments = calCalendar.getNumberOfImportantAppointments();
if (integerNumberOfImportantAppointments > 0)
{
   // itoa is not standard (like any of this is)
   WCHAR strN[32];
   swprintf(strN, L"%d", integerNumberOfImportantAppointments);

   // not familiar with ShowMessage(), but I *think* this will work.
   ShowMessage("You have " + UnicodeString(strN) + " important appointments. Do you wish to view them?");
}