Writing a simple calculator, trouble with the decimal

Hi, Im new to C++, Im trying to write a calculatop app.
Im using MVC++ and using the windows form designer to make the GUI,

I have it working with simple integers but cannot undestand a way to work with floats using the decimal,

I have the buttons create a string in a text box, then convert the string to an integer, do the math and then convet the resault back to a string to display in the text box,

Question, how can i convert a string eg: 12.00 into a float to do math on.
here is a few snippets of my code to see what I am doing wrong!




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
enum eMyoperator
		{
			null = 0,
			plus = 1,
			minus = 2,
			times = 3,
			divide = 4,
		};

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
			 //this is one of my buttons to populate the text box
			 this->out->Text = this->out->Text + "1";
		 }
private: System::Void buttonClr_Click(System::Object^  sender, System::EventArgs^  e) 
		 {	
                         // my clear button to reset m_nValue1 and m_nValue2
                         //
			 m_nValue1 = 0, m_nValue2 = 0;
			 eOp = null;
			 this->out->Text = "";

private: System::Void buttonPlus_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
			 String^ sTemp = this->out->Text;
			 __int32 nValue =  System::Int32::Parse(sTemp);
			 m_nValue1 = nValue;
			 eOp = plus;
			 this->out->Text = "";
		 }
//My Equals button
private: System::Void buttonEqu_Click(System::Object^  sender, System::EventArgs^  e) 
		 {
			 String^ sTemp = this->out->Text;
                         // assiging the text in the box to a temp string
			__int32 nValue =  System::Int32::Parse(sTemp);
                        // converting the string to an integer
			if (m_nValue2 == 0)
                        m_nValue2 = nValue;
                        // chack so i can use the '=' button over and over and step the resault!

                        // switch to do math
			switch(eOp)
			{
			case 1:
				m_nValue1 += m_nValue2;
				
				break;
			case 2:
				m_nValue1 -= m_nValue2;
				break;
			case 3:
				m_nValue1 *= m_nValue2;
				break;
			case 4:
				m_nValue1 /= m_nValue2;
				break;
			default:
				this->out->Text = "ERROR";
			};
			std::stringstream out;
			out << m_nValue1;
                        // convert m_nValue back to a string to be displayed in the Text box (out)
			String^ str = gcnew String (out.str().c_str());
			this->out->Text = str;
                        // output resault in text box
			delete str; // delete str? not sure if i need to do this.

		}





Last edited on
no bother i solved this one on my own
wrote a container class for the operands and stored them as floats

void set(String^ str)
{
float m_fOperand = Single::Parse(str);
}

// convert the float back to a String^
// c1.get() returns a float
String^ resault = System::Convert::ToString(c3.get());

problem solved!
Topic archived. No new replies allowed.