I tried to utilize the square root symbol in my Windows Form calculator using a ComboBox. Excuse me for the messy code:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
float x, y, z;
bool err = false;
x = int::Parse(textBox1->Text);
y = int::Parse(textBox3->Text);
if (comboBox1->SelectedItem->ToString() == "+") z = x + y;
else if (comboBox1->SelectedItem->ToString() == "-") z = x - y;
else if (comboBox1->SelectedItem->ToString() == "/") z = x / y;
else if (comboBox1->SelectedItem->ToString() == "*") z = x * y;
else if (comboBox1->SelectedItem->ToString() == "^") z = pow(x, y);
else if (comboBox1->SelectedItem->ToString() == "√") z = sqrt(x);
// if (comboBox1->SelectedItem->ToString() == "/" && y == 0) err = true;
if (err == true) textBox4->Text = "error";
else textBox4->Text = z.ToString();
}
I simply used the Unicode symbol in both the code and the ComboBox items list. It should have passed the if
statement and simply squared x
and given z
, however the program just does not calculate anything. By my guess, it doesn't pass the if
statement, because if I replace the square root symbol with a regular character then it works perfectly.
Yes, the file is saved with Unicode encoding, as it shows up well on my program when launched.
Whilst it isn't a direct fix, i have found a workaround utilising selectedindex, that way instead of comparing a unicode character it just checks if the item is selected. works flawleslly in my code
if (comboBox1->SelectedIndex == 0) z = x + y;