How would i make a messagebox display a variables contents, right now it gives me compile 'cant convert string to char*', and it gives me the smae error when i give it a char* or the & of either.
Printable View
How would i make a messagebox display a variables contents, right now it gives me compile 'cant convert string to char*', and it gives me the smae error when i give it a char* or the & of either.
Show me how you call your messagebox
i think strings might need to use the string.c_str()
i think the string class has that :)
#include <windows.h>
#include <iostream.h>
#include <string>
int main()
{
string name;
cout<<"Whats your name?\n";
cin>>name;
MessageBox(NULL, name, name, MB_OK);
return 0;
}
thats the chunk
try:Quote:
Originally posted by Mushroom Realm
#include <windows.h>
#include <iostream.h>
#include <string>
int main()
{
string name;
cout<<"Whats your name?\n";
cin>>name;
MessageBox(NULL, name, name, MB_OK);
return 0;
}
thats the chunk
PHP Code:MessageBox(NULL, name.c_str(), name.c_str(), MB_OK);
Replace iostream.h with iostream, and add a using namespace std; (use Google to find out, or look round the forums for why).
Yes, because the new string overloads the operator char * () right parksie?
Nope. There's a good reason why the standard string does *not* have an implicit conversion to char*.
It messes up all the type-detection, so in the end it's much less useful for optimisations.
coool
Quite confusing for people who only used CString all the time...
Yet it does make a lot of sense.
BTW if you care for UNICODE compatibility (easier localization and faster execution in WinNT/2k/XP) you should use this instead of string:
typedef basic_string<TCHAR> tstring;
You must still include <string>.
Use tstring just like string, but also include <tchar.h> and always use the _T() macro to wrap all your literal strings.
tstrign mystr = _T("Hello, UNICODE!");