As the title states I'm building a calculator. I've got two arrays, one for holding the values input by the user, and the other for holding the operators. Addition seems to work just fine, however subtraction, multiplication, and division are still putting up a fight. I believe it's just logic errors inside my code for the equals method because that's where all the math is done anyways. So here are the problems:
Subtraction: When subtracting a value like 3 from 15 it returns a negative 12. The math is right but it's displaying the incorrect sign.
Multiplication and Division: The problem with these two are the same. No matter how many values I input, when equals is pressed it returns the last value entered automatically. I can't figure out any reason for that at all.
Here is the code for the equals method, fully commented with my intentions. Maybe someone here is smart enough to help me on this. Much thanks to all who help.
private void Equals_Click(object sender, EventArgs e)
{
// Change all values to the decimal number system to perform math.
ChangeToDecimal(sender, e);
// Set the result variable to 0 to ensure correct math.
Results64 = 0;
// Set the current value to the one entered by the user before the equals button was clicked.
Current64 = Convert.ToInt64(InputDisplay1.Text);
for (int i = 0; i < InputIndex; i++)
{
// Perform addition if the currently selected operator is addition.
if (Operators[i] == "+")
{
if (InputIndex == 0)
Results64 = Input64[i];
else
Results64 += Input64[i];
}
// Perform subtraction if the currently selected operator is subtraction.
else if (Operators[i] == "-")
{
if (InputIndex == 0)
Results64 = Input64[i];
else
Results64 -= Input64[i];
}
// Perform multiplication if the currently selected operator is multiplication.
else if (Operators[i] == "*")
{
if (InputIndex == 0)
Results64 = Input64[i];
else
Results64 *= Input64[i];
}
// Perform division if the currently selected operator is division.
else if (Operators[i] == "/")
{
if (InputIndex == 0)
Results64 = Input64[i];
else
Results64 /= Input64[i];
}
}
// Perform final arithmatic.
if (Operators[InputIndex] == "+")
Results64 += Current64;
else if (Operators[InputIndex] == "-")
Results64 -= Current64;
else if (Operators[InputIndex] == "*")
Results64 *= Current64;
else if (Operators[InputIndex] == "/")
Results64 /= Current64;
// Display the results.
InputDisplay1.Text = Results64.ToString();
InputDisplay2.Text = "";
// Clean out the arrays. (Thinking about just re-initializing them instead of a loop).
for (int i = 0; i < InputIndex; i++)
{
Operators[i] = "";
Input64[i] = 0;
}
// Set the index back to zero.
InputIndex = 0;
}