none
Display an int in a MessageBox?

    Question

  • Hi,

    I'm new to C++, have started to use VC++ 2003 at work and dont know much about it because i have only used Java and VB before now.  I cant use the debugger for reasons that arent relevant here but all i want to do is use a MessageBox to display an int!  I cant seem to do this no matter what i try.  I dont care if i have to use the AfxMessageBox function instead but please some one tell me how its done!  I'll give you the example below

    int back_color = 2;

    how do i display that 2 or rather back_color in a messagebox?  thats all i am asking!


    Many thanks

    Will
    Wednesday, August 31, 2005 2:37 PM

Answers

  • Hi Will,

    You have to pass a string to the MessageBox APIs.  Here's an example of how to get a string to display the int value in VS 2005 (if you're using 2003, just remove the "L" in front of the first argument to s.Format):

    CString s;
    s.Format(L"%d",1);
    AfxMessageBox(s);

    Hope this helps,

    -Ron Pihlgren
     VC++ Testing
    Wednesday, August 31, 2005 4:27 PM

All replies

  • Hi Will,

    You have to pass a string to the MessageBox APIs.  Here's an example of how to get a string to display the int value in VS 2005 (if you're using 2003, just remove the "L" in front of the first argument to s.Format):

    CString s;
    s.Format(L"%d",1);
    AfxMessageBox(s);

    Hope this helps,

    -Ron Pihlgren
     VC++ Testing
    Wednesday, August 31, 2005 4:27 PM
  • Thanks, although i am using 2003 and i had to keep the 'L' in, so i take it FORMAT is a method in the CString class?

    Thanks again

    Will

    Thursday, September 01, 2005 8:48 AM
  • Yes, format is a method in the CString class.

    The "L" is needed if you are building for Unicode.  I just mentioned it since the default for VS2003 was ANSI and the default for VS2005 is Unicode.  You can change the defaults on either platform.

    I'm glad this helped.

    -Ron
    Thursday, September 01, 2005 10:32 PM