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;
        }

Make a subtract function that takes two parameters (you can even use generics, if you want).

Make the subtraction action return the result.
Use only that function for subtraction.
Don't overcomplicate it.

I'll try re-writing the whole app, that usually helps me.

Is inputLength the length of the inputs array? Because I don't see how the two arrays would be of the same length. If I wanted to subtract 3 from 15 I'd have 2 numbers in the input array and only one in the operators array (the - sign). Yet you loop through them both at the same rate.
Is input1 not in the input array?
To be honest I can't see why the arrays are needed, most calculators perform the operation on the 2 inputs, storing the result awaiting the next operator.

I'm not specifically talking about a re-write, but when it comes down to subtraction, there should be only three things that come into play -- the 1st arg, the 2nd arg and the result.

Everything else can feed those parameters or receive the result, but the action is always the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.