using std::string with gui elements

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
agrif

using std::string with gui elements

Post by agrif »

Taking the advice from a previous post, I have started using the std::string class, along with string streams. This is VERY useful, and very easy to use.

The time has come for me to add a simple GUI to my game, and, of course, cannot use string.

All of the GUI elements use wchar_t* as their format. How do you get this format from std::string?
Mercior
Posts: 100
Joined: Tue Feb 24, 2004 1:53 am
Location: UK
Contact:

Post by Mercior »

There is a core::stringw class which holds wide character strings, and you can get a constant array of its characters with string.c_str(), however, I have had no luck in getting text into a GUI element either, even with this method :(
saigumi
Posts: 921
Joined: Fri Aug 22, 2003 11:31 am
Location: St. Louis, MO USA
Contact:

Post by saigumi »

wchar_t* is a Wide Character Pointer, while a char* is a Multibyte Character Pointer.

The functions to go between the two are mbstowcs() and wcstombs().

Alternately, you can use mbtowc() and wctomb() to only convert 1 character.

This is one way I do it. Another way would be to malloc the storage size. Since c_str() just returns a const char*, replace somechar* with your c_str()'ed string

Code: Select all

wchar_t ConvText[255];
mbstowcs(ConvText, somechar*,255);
Crud, how do I do this again?
agrif

Post by agrif »

Thanks. I looked up mbstowcs() on google, and used some examples.

However, when I compile, there are no errors, but then I run it, and it gives me a nice crash.

Here is my code, but I dunno if I'm doing anything wrong.

Code: Select all

// tester! yay!
#include <iostream>
using namespace std;
int main() {
cout << "type a string to convert!" << endl << ">: ";
string str("");
cin >> str;

wchar_t *charw;
mbstowcs(charw, str.c_str(), 255);

cout << "Wchar'd string: " << charw << endl;

return 0;
}
I am using the latest version of Dev-C++, if that has anything to do with it.

agrif
Tels
Posts: 65
Joined: Fri Feb 27, 2004 7:56 pm
Location: Antarctica
Contact:

Post by Tels »

agrif wrote:Thanks. I looked up mbstowcs() on google, and used some examples.

However, when I compile, there are no errors, but then I run it, and it gives me a nice crash.

Here is my code, but I dunno if I'm doing anything wrong.

Yes you do :)

See:

Code: Select all

// tester! yay!
#include <iostream>
using namespace std;
int main() {
cout << "type a string to convert!" << endl << ">: ";
string str("");
cin >> str;

wchar_t *charw;
mbstowcs(charw, str.c_str(), 255);
Note! You did not allocate memory for the wchar string buffer here!

Change that to:

Code: Select all

wchar_t charw[255];
mbstowcs(charw, str.c_str(), 255);
And it should work.

HTH,

Tels :twisted:
Perl + Irrlicht + Audiere = Game: http://bloodgate.com/perl/game
agrif

Post by agrif »

when I do that, it prints out the location in the memory of the computer, not the actual value. I also figured that I would do it that way, because the description I found online was:

mbstowcs(wchar_t *pwcs, const char *s, size_t n)

I am starting to REALLY hate pointers :) .

agrif
Tels
Posts: 65
Joined: Fri Feb 27, 2004 7:56 pm
Location: Antarctica
Contact:

Post by Tels »

agrif wrote:when I do that, it prints out the location in the memory of the computer, not the actual value. I also figured that I would do it that way, because the description I found online was:

mbstowcs(wchar_t *pwcs, const char *s, size_t n)

I am starting to REALLY hate pointers :) .

agrif
Use Perl :)

Anyway, look up what the string class does and what function it provides. I couldnt find it via man on my system, and I don't have any C++ environment AFAIK. You want a member of str that returns you a pointer to the actual string contents.

Best wishes,

Tels
Perl + Irrlicht + Audiere = Game: http://bloodgate.com/perl/game
Post Reply