locked
Calculate more than 2 numbers RRS feed

  • Question

  • I am writing a calculator program but have hit a hurdle.
    I have got it to ADD so far value 1 and value 2 (stored in variables).
    It performs the calculation perfectly for the first 2 numbers, right?

    *

    For example...I click button2 then click AddBtn then click button5.
    Then I click equalsBtn and it shows in the texbox 7 (that's correct!).

    *

    NOW, I go on to press equalsBtn again and instead of adding 5 to 7.
    It adds 2 to 7. This is the first of two problems I am experiencing. :-/

    SECOND PROBLEM occurs when I press AddBtn again.
    (after doing the calculation shown between * symbol).

    Say I press AddBtn then button3 for the sake of argument.
    I expect it to now add 3 to the previous calculation of 7, ok?
    Instead, what it does, is it displays on screen 73. Why?! :-/

    THE CODE I AM USING:

    (Variables)

    // variables to hold operands
            double valHolder1;
            double valHolder2;
            double answer;
            // variable to hold Operater
            string calcFunc;

    (Calculator Function)

    private void CalculateTotals()
            {
                valHolder2 = System.Double.Parse(output.Text);
                // determine which calculation we're going to execute
                // by checking the value of calcFunc
                switch (calcFunc)
                {
                    // addition
                    case "Add":
                        answer = valHolder1 + valHolder2;
                        break;
                    // subtraction
                    case "Subtract":
                        answer = valHolder1 - valHolder2;
                        break;
                    // division
                    case "Divide":
                        answer = valHolder1 / valHolder2;
                        break;
                    // multiplication
                    case "Multiply":
                        answer = valHolder1 * valHolder2;
                        break;
                    // exponents (power of)
                    case "PowerOf":
                        answer = System.Math.Pow(valHolder1, valHolder2);
                        break;
                }
                // set our input area to the value of the calculation
                output.Text = answer.ToString();
            }

    My Add Button (actually called plusBtn not AddBtn)

    private void plusBtn_Click(object sender, RoutedEventArgs e)
            {
                // Make sure out input box has a value
                if (output.Text.Length != 0)
                {
                    // Check the value of our function flag
                    if (calcFunc == string.Empty)
                    {
                        // Flag is empty
                        // Assign the value in our input
                        // box to our holder
                        valHolder1 = System.Double.Parse(output.Text);
                        // Empty the input box
                        output.Text = string.Empty;
                    }
                    // Assign a value to our calc function flag
                    calcFunc = "Add";
                }
            }

    Wednesday, March 13, 2013 6:45 PM

Answers

  • For part 1, try putting valholder1 in answer variable and then use answer variable inside Calculatetotals, so your code for add will be answer = answer + valholder2. Now after second click, the last value will be repeatedly added to the last answer if this is the behavior you are looking for. :)

    There is one problem in suggested fix that valholder2 is also getting updated in eqlbtn click, so it will not work. 


    Thanks, Sachin


    • Edited by Sachin S Friday, March 15, 2013 2:00 PM
    • Marked as answer by Min ZhuMember Thursday, April 4, 2013 6:59 AM
    Wednesday, March 13, 2013 7:41 PM

All replies

  • For part 1, try putting valholder1 in answer variable and then use answer variable inside Calculatetotals, so your code for add will be answer = answer + valholder2. Now after second click, the last value will be repeatedly added to the last answer if this is the behavior you are looking for. :)

    There is one problem in suggested fix that valholder2 is also getting updated in eqlbtn click, so it will not work. 


    Thanks, Sachin


    • Edited by Sachin S Friday, March 15, 2013 2:00 PM
    • Marked as answer by Min ZhuMember Thursday, April 4, 2013 6:59 AM
    Wednesday, March 13, 2013 7:41 PM
  • Hi,

    I can't find anything wrong with your code. I would suggest you to use breakpoint and Step Into/Step Over combined with Watch window to find out what has happened.


    Aaron
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 15, 2013 7:23 AM
  • Aaron, Are you sure part 1 is okay, because there seem to be a problem as result Quiver is getting is inline with code execution flow?

    As clearly there is only one place valHolder1 = System.Double.Parse(output.Text); (2 in this case) is being initialized in code and that is add btn. So if Quiver clicks "Equal" then all code is going to do is take last displayed value (valHolder2 = System.Double.Parse(output.Text);) and update valHolder2(5 in this case), and then add valHolder1 to valHolder2 (as calcFunc is Add, so result is 2 + 5 = 7).  

    Next "Equal" button click will just update valHolder2 to 7 (valHolder2 = System.Double.Parse(output.Text);) and add to valHolder1 (which is still 2) and result will be 2 + 7 = 9. This what he is observing in his side.

    So to add 7 + 5, he needs to update the logic. He needs to avoid update of valHolder2 in next "Equal" button click and then add it to last answer. 


    Thanks, Sachin

    Friday, March 15, 2013 1:59 PM