What is the problem with the following code?
MessageBox(NULL,"The number is: "x,"Msg",MB_OK);
(x is the integer variable)
Printable View
What is the problem with the following code?
MessageBox(NULL,"The number is: "x,"Msg",MB_OK);
(x is the integer variable)
You need to add a comma before starting the argument list.
Quote:
MessageBox(NULL,"The number is: ",x,"Msg",MB_OK);
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.
Or boost::lexical_cast:
MessageBox(NULL,("The number is: " + boost::lexical_cast<std::string>(x)).c_str(),"Msg" ,MB_OK);
I tried to copy the integer into the string usingbut it didnt work.Code:strcat(text, x);
What am I doing wrong?
Because strcat works with strings! And never use strcat in C++.
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.
Spidey, out of curiosity, do you know how to program C/C++?
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.
Read this.
Here's an example:
Code:std::string mynumstr = "The number is: ";
mynumstr += x;
MessageBox(NULL, mynumstr.c_str(), "Msg", MB_OK);
Quote:
Both, Why do you ask ?
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.Quote:
You need to add a comma before starting the argument list.
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.Quote:
MessageBox(NULL,"The number is: "x,"Msg",MB_OK);
Code:int x = 5;
char text[32]
sprintf(text,"The number is %d",x);
MessageBox(0,text,"Msg",MB_OK);
And I'm sure there are other ways as well. Use what works.Code:int x = 5;
std::ostringstream ostrm;
ostrm << "The number is " << x << std::endl;
MessageBox(0,ostrm.str().c_str(),"Msg",MB_OK);
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);
Yes but that does not give you the number since you passed in a string literal.
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).