Putting float values in an editbox

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
2camjohn2

Putting float values in an editbox

Post by 2camjohn2 »

I have data stored in my classes as floats. I want to put this data into an edit box to allow it to be altered.

I have managed to get the data from the editbox into my class but I cant do it the other way round.

i know the editbox reqires a const wchar_t but I cant seem to get my floats into one.
Ive tried casting them to strings, and to ints, and even to ints then strings but I always get errors and it shows up in my editbox as squares not numbers.

If anyone can provide a quick example of how to convert a float into a const wchar_t for use in an editbox I would be very grateful.

Thanks
John
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

I've been working a bit with editbox myself...

I'm using whole numbers but at one point was faking the first set like..

env->addEditbox ( L"1.0"

such as the mesh viewer..I can see how this wouldn't be a solution though..

let me play a bit see what I can do.
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

Code: Select all

						// set scale
						gui::IGUIElement* root = env->getRootGUIElement();
						core::vector3df scale;
						core::stringc s;

						s = root->getElementFromId(901, true)->getText();
						scale.X = (f32)atof(s.c_str());
						s = root->getElementFromId(902, true)->getText();
						scale.Y = (f32)atof(s.c_str());
						s = root->getElementFromId(903, true)->getText();
						scale.Z = (f32)atof(s.c_str());

						if (Model)
							Model->setScale(scale);
the above is taken from the meshviewer I tested the meshviewer to see if 1.5 scale would work..it does.

post some code and I'll make it work.
hybrid

Post by hybrid »

Well, if I got the original message correct the way from wchar_t to float was solved, but not the other way round. Should work with swprintf which lets you format a wide char string similar to sprintf for usual characters. Try

Code: Select all

swprintf(wchar_buf, 256, "%f", floatvar)
where wchar_buf is a previously allocated memory range and floatvar contains your floats. You can then add this string to the GUI elements.
Guest

Post by Guest »

ok in guice swprintf is used for saving in the exact same way you use it here
however I'm curious what is the second perimeter "256" for?

I assume it's for the size of the wchar_buf?

swprintf(wchar_buf, 256, "%f", floatvar)
hybrid

Post by hybrid »

Yes, it's to prevent buffer overrun. You will probably use a shorter buffer, though, and restrict the length with additional precision arguments to the format string. Check the swprintf man pages for that.
2camjohn2

Post by 2camjohn2 »

Thanks for the help so far guys.

Yeah hybrid you got it right. I want to put floats in the editbox. I have already done it the other way round (by nicking the code from meshviewer).


I still cant get it to work though. I suspect I am not using the wchar_t properly.

Here is the test code:

Code: Select all

	float floatvar = 12;
	wchar_t *s = new wchar_t();
	swprintf(s, 256, "%f", floatvar);
It fails to compile with this error
: error C2664: '_snwprintf' : cannot convert parameter 3 from 'char [3]' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.


Thanks alot for the help
John
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

just a guess, I've never user wchar_t before and I'm not in front of a compiler, but it looks to me like you need an array...

Code: Select all

   float floatvar = 12; 
   wchar_t s[256]; 
   swprintf(s, 255, "%f", floatvar);
2camjohn2

Post by 2camjohn2 »

Thanks for helping Bitplane, but I tried what you said and I still get the same compiler error.



John
2camjohn2

Post by 2camjohn2 »

Im getting the same problem trying to put a string into an editbox.

I gues im missing something about C++ string formatting.

If anyone can point me in the direction of a good online resource about this topic, I would be grateful.

Also if someone can please fix my code so it works, Im sure I would learn something that way too.

Thanks
John
hybrid

Post by hybrid »

Sorry, my fault. It should be L"%f". The L makes this string a wide char string which is needed for the *wprintf functions. That's because you might want to use constant wide chars in your formatting string.
pauly
Posts: 10
Joined: Wed Mar 23, 2005 11:29 am
Location: Belgium
Contact:

Post by pauly »

Post Reply