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!
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.
}