Displaying a variable in a msg box.

This is a discussion on Displaying a variable in a msg box. within the C++ Programming forums, part of the General Programming Boards category; What is the problem with the following code? MessageBox(NULL,"The number is: "x,"Msg",MB_OK); (x is the integer variable)...

  1. #1
    Registered User kakayoma's Avatar
    Join Date
    Jul 2009
    Location
    Dallas, Texas
    Posts
    42

    Question Displaying a variable in a msg box.

    What is the problem with the following code?




    MessageBox(NULL,"The number is: "x,"Msg",MB_OK);

    (x is the integer variable)

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    You need to add a comma before starting the argument list.

    MessageBox(NULL,"The number is: ",x,"Msg",MB_OK);
    Spidey out!

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Except I don't think you can have five arguments, can you?

    You'll need to build a string that has the information you want to use in it. Since you posted this in C++, probably that means you want to use a stringstream.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Or boost::lexical_cast:
    MessageBox(NULL,("The number is: " + boost::lexical_cast<std::string>(x)).c_str(),"Msg" ,MB_OK);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User kakayoma's Avatar
    Join Date
    Jul 2009
    Location
    Dallas, Texas
    Posts
    42

    Post

    I tried to copy the integer into the string using
    Code:
    strcat(text, x);
    but it didnt work.

    What am I doing wrong?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Because strcat works with strings! And never use strcat in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,627
    strcat() copies one string to another, to use it, you have to convert your int into string from. It's easiest to use the STL, but you can use something like sprintf() to itoa() if you want to.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  8. #8
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,627
    Spidey, out of curiosity, do you know how to program C/C++?
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Itoa is non-standard. Plus string streams and boost's lexical_cast works for all kinds of conversions. String streams converts anything into a string, and lexical_cast converts anything to anything.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,627
    Read this.

    Here's an example:
    Code:
    std::string mynumstr = "The number is: ";
    mynumstr += x;
    MessageBox(NULL, mynumstr.c_str(), "Msg", MB_OK);
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  11. #11
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by Yarin View Post
    Spidey, out of curiosity, do you know how to program C/C++?
    Both, Why do you ask ?
    Spidey out!

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Both, Why do you ask ?
    You need to add a comma before starting the argument list.
    Because you made it sound as if MessageBox acted something like C sprintf and a VB string. Neither of those is correct and your suggestion was way off the mark.

    MessageBox(NULL,"The number is: "x,"Msg",MB_OK);
    The string in C/C++ is "The number is:" - the x is ignored and that line will not compile. "Msg" is the title of the message box and MB_OK is the style. x has no meaning except maybe in a VB string where auto-concatenation works.

    Code:
    int x = 5;
    char text[32]
    sprintf(text,"The number is %d",x);
    MessageBox(0,text,"Msg",MB_OK);
    Code:
    int x = 5;
    std::ostringstream ostrm;
    ostrm << "The number is " << x << std::endl;
    MessageBox(0,ostrm.str().c_str(),"Msg",MB_OK);
    And I'm sure there are other ways as well. Use what works.
    Last edited by VirtualAce; 08-02-2009 at 06:15 PM.

  13. #13
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Ah, I see. But you do require a comma after each argument as far as the syntax goes right ? even though the above code is wrong.

    for example, without the x it would be
    Code:
    MessageBox(NULL,"The number is: ", "Msg" ,MB_OK);
    Spidey out!

  14. #14
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Yes but that does not give you the number since you passed in a string literal.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I think the question they meant to ask was "Spidey, how much Windows programming have you done?" (since I imagine there are C/C++ programmers who have gone their whole lives without using MessageBox).

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Virtual Box
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2009, 04:08 AM
  3. No data showing in combo box
    By PJYelton in forum Windows Programming
    Replies: 6
    Last Post: 04-29-2005, 07:20 PM
  4. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM
  5. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM

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