Thread: Displaying a variable in a msg box.

  1. #16
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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).
    Hehe. Quite true. Nice catch. But since MessageBox() takes two LPCTSTRs one would have to know how to pass in C style strings in order to get it to work for them. So in this instance I can see how some might question a person's knowledge of C/C++ if they did not know how to get MessageBox() to display what they wanted.
    Last edited by VirtualAce; 08-02-2009 at 06:25 PM.

  2. #17
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Yes but that does not give you the number since you passed in a string literal.
    Yes, thats what I meant.

    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).
    That would be little to none.
    Spidey out!

  3. #18
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That would be little to none.
    So why are you answering a question that involves a Win32 function if you have little to no experience with Win32?

  4. #19
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Well, I just saw the missing comma which looked suspicious so I assumed it would be wrong anyway.
    Spidey out!

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

    Exclamation

    I've already tried to use sprintf() but if i put in the following code :

    Code:
    sprintf(text,"The num is :",x);
    The message box only displays the text but it doesn't disply the int.

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

    Exclamation

    I've already tried to use sprintf() but if i put in the following code :

    Code:
    sprintf(text,"The num is :",x);
    The message box only displays the text but it doesn't disply the int.

  7. #22
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Thats because your never storing the value of x in the text. You need a format specifier, like this

    Code:
     sprintf(text,"The num is : %d",x);
    Spidey out!

  8. #23
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    This is a C++ forum. Why don't you use C++?

    Code:
    #include <string>
    #include <sstream>
    
    /* ...*/
    
    	std::string msgtxt = "The num is : ";
    	std::ostringstream ostr;
    	int x = 13;
    
    	ostr << msgtxt << x; // concatenation happens here
    
            //c_str() because ostringstream.str() returns a std::string
            MessageBox(NULL, ostr.str().c_str(), "Msg", MB_OK);
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

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

    Exclamation Help -Again!

    Well my first problem is over but now I have another one:

    Code:
    		sprintf(text1,"x is : % d", x);
    		sprintf(text2,"Y is : % d", y);
    		strcat(main, text1);
    		strcat(main, "\n\n");
    		strcat(main, text2);
    		MessageBox(NULL, main , "msg",MB_OK);
    When i do this and test it the msg box shows this

    D[]ox is : 4

    y is: 5

    why does is show the 3 charachters at the beginning ?

    When i do this it works perfectly but only shows one string :

    Code:
    		sprintf(text1,"x is : % d", x);
    		sprintf(text2,"Y is : % d", y);
    		strcat(main, text1);
    		strcat(main, "\n\n");
    		strcat(main, text2);
    		MessageBox(NULL, text1 , "msg",MB_OK);

    Vista - Something We all Love to Hate.

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because strcat adds to the end of the string. Since main already contains stuff, the new "x is:" is added to the end.

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

    Exclamation

    Main doesn't contain any thing i checked and rechecked

    P.s.The three charachters are special charchters described below:


    THE FIRST IS A Dwith a line through it

    The second is a square(not 2 brackets)

    and the third is the letter o


    See signature for picture

    Vista - Something We all Love to Hate.

  12. #27
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    >> Main doesn't contain any thing i checked and rechecked
    You may not have put anything in main, but when your program allocated the memory for main during run-time, the contents of main were left undefined - basically, there's never any telling what your allocated memory's content will start out as! That's why you're suppose to zero out main first, replace the first strcat() with strcpy(), or, just use std::string as we continue to advice you to do instead.

    Oh, and I wouldn't use the name of the your entry point function for other things.

  13. #28
    Registered User kakayoma's Avatar
    Join Date
    Jul 2009
    Location
    Dallas, Texas
    Posts
    42
    Quote Originally Posted by Yarin View Post
    You may not have put anything in main, but when your program allocated the memory for main during run-time, the contents of main were left undefined - basically, there's never any telling what your allocated memory's content will start out as! That's why you're suppose to zero out main first, replace the first strcat() with strcpy(), or, just use std::string as we continue to advice you to do instead.
    Thanks

    Vista - Something We all Love to Hate.

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And why do you still insist on using the C approach!?!
    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.

  15. #30
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    And why do you still insist on using the C approach!?!
    Either approach is valid whether it be via stringstream or via sprintf. Since this is a C++ forum then naturally we would recommend the stringstream approach. However that does not make it more or less right in a purely technical sense. Both will accomplish the goal and both will get the job done. To me there is no benefit in using C++ just to be more C++-ey and in this instance it makes no difference either way. When I use a C++ approach to solve a problem it is for a very distinct reason and to serve a specific purpose. It is not merely to please the C++ gods whoever they may be or to look myself in the mirror in the morning and pride myself on being a C++ programmer.
    Last edited by VirtualAce; 08-03-2009 at 10:17 PM.

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, 05: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