0

I had been displaying String uptill now using messageBox(). How can I display an integer using this function? I tried something like this but it didn't work:

int message=1;
MessageBox(NULL,
           (LPCSTR)message,
           "Display",
            MB_ICONINFORMATION);    
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Ayse
  • 2,676
  • 10
  • 36
  • 61

1 Answers1

4

You need to place the int into a string. In C, you can use sprintf():

char buffer[32];
sprintf(buffer, "%d", message); 
MessageBox(NULL, buffer, "Display", MB_ICONINFORMATION);

and in C++ there are several options (see Append an int to a std::string for suggestions) for storing an int in a std::string then use std::string::c_str() in the call to MessageBox().

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • I am working on WIN32 API using C and I want my data to be displayed on Window that is why I was using MessageBox(). Can sprintf() be used to display data on a Window? :( – Ayse Mar 15 '13 at 11:22
  • 1
    @AyeshaHassan Read and understand the answer. Learn what `sprintf` does. It outputs text to a buffer. What you do with that text is up to you. One possibility is to send it to `MessageBox`. – David Heffernan Mar 15 '13 at 13:02
  • @hmjd: Thank you so much for hte help. Your answer solved my problem :) – Ayse Mar 18 '13 at 06:16