How do you add strings and numbers to be shown in a MessageBox?

This is a discussion on How do you add strings and numbers to be shown in a MessageBox? within the C++ Programming forums, part of the General Programming Boards category; For example, have the message box say, "x equals 5, and that's just dandy"....

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    How do you add strings and numbers to be shown in a MessageBox?

    For example, have the message box say, "x equals 5, and that's just dandy".

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Code:
    #include <string>
    
    [...]
    
    std::string MessageText = "X equals ";
                MessageText += X;
                MessageText += ", and that's just dandy";
    
    MessageBox(hWnd, "Title", MessageText.c_str(), MB_OK);

  3. #3
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    I posted about this awhile ago... Oh well, here is the first code that I got a hold of:

    Code:
    #include <sstream>
    #include <windows.h>
    
    int main( void )
    {
      int X = 5; // If this is what you mean't
      std::stringstream Message;
      Message << "X equals " << X << ", and that's just dandy.";
      MessageBox( NULL, Message.str().c_str(), "Title", MB_OK );
      return 0;
    }
    I use this way because it's the first way I got (and it works fine).

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by stovellp
    Code:
    #include <string>
    
    [...]
    
    std::string MessageText = "X equals ";
                MessageText += X;
                MessageText += ", and that's just dandy";
    
    MessageBox(hWnd, "Title", MessageText.c_str(), MB_OK);
    Are you sure? I thought you could only add strings and characters to a string. Any other types have to be added through a stringstream.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 12-05-2008, 01:16 PM
  2. Replies: 5
    Last Post: 12-21-2007, 12:38 PM
  3. strings and numbers
    By jrahhali in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2003, 12:24 PM
  4. Replies: 13
    Last Post: 01-22-2002, 03:38 AM
  5. File Access with Strings and Numbers
    By Mace in forum C Programming
    Replies: 3
    Last Post: 12-01-2001, 03:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21