Converting textbox string to float?
Solution 1
Fixing it for you,
String^ i1 = textBox1->Text;
float rez = (float)(Convert::ToDouble(i1)*4.35);
textBox2->Text = rez.ToString();
Basically, you want to convert your string to an actual number, do the math, and then make it back into a string for displaying purposes.
Solution 2
You're trying to multiply a string by a double and there is no operator that defines how to do that. You need to convert your string to a double first, and then use that in the calculation.
Then, you're trying to assign a string to a float, which again is nonsense.. You need to calculate the float, then convert it to a string when assigning it to the textbox text field.
Something like:
String^ i1 = textBox1->Text;
float rez = (Convert::ToDouble(i1)*4.35);
textBox2->Text = rez.ToString();
Bugster
I'm no longer active due to the exagerated moderation of this site. Moderation is great but after seeing questions of over 500 votes be closed because they're not constructive, I've come to question it. What if some day all closed questions get purged?
Updated on June 15, 2022Comments
-
Bugster almost 2 years
I'm basically trying to write a basic converter in visual studio 2008, and I have 2 text boxes, one which gets input from the user, and one which gives output with the result. When I press the button I want the input from the first textbox to multiply by 4.35 then display in the 2nd textbox. This is my code in the button code so far:
String^ i1 = textBox1->Text; float rez = (i1*4.35)ToString; textBox2->Text = rez;
However I'm getting these errors:
f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(148) : error C2676: binary '*' : 'System::String ^' does not define this operator or a conversion to a type acceptable to the predefined operator f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(148) : error C2227: left of '->ToString' must point to class/struct/union/generic type f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(149) : error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'float' to 'System::String ^'
Please help I'm going insane on how ridiculously difficult it is to get some input from a textbox in C++. I've googled every error I had and nothing useful came up, I've been searching answers for an hour already, please help.
-
Bugster over 12 yearsIt works! Thank-you. I found in Stackoverflow in 10 minutes what I was looking for an hour. Thanks again!
-
Blindy over 12 yearsYep, I'm a fan of this site too :)
-
Bugster over 12 yearsYes it's truly amazing, I'll accept your answer in 3 minutes (When the timer lets me do it) EDIT: Accepted, thanks once more.
-
Bugster over 12 yearsThanks, but my question was already answered, still you deserve a +1 for your effort.
-
StevieG over 12 yearsYeah, you gotta be quick on here! :)