none
calculator problem RRS feed

  • Question

  • I'm new to C# and I'm having difficulties with this calculator code. Everything works find until you try to do something with the result of the calculation. For example if i do 8*2 and get 16, i cant add/subtract/multiply/divide from this result. Here is the code. 

    namespace Calculator
    {
        public partial class Form1 : Form
        {
            string input = string.Empty;
            string operand1 = string.Empty;
            string operand2 = string.Empty;
            char operation;
            double result = 0.0;

            public Form1()
            {
                InitializeComponent();
            }

            private void Zero_Click(object sender, EventArgs e)
            {
                if (textBox1.Text == "")
                {
                    this.textBox1.Text = "";
                    input += "0";
                    this.textBox1.Text += input;
                }
                else if (textBox1.Text == "0")
                {

                }

            }

            private void One_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "1";
                this.textBox1.Text += input;
            }

            private void Two_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "2";
                this.textBox1.Text += input;
            }

            private void Three_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "3";
                this.textBox1.Text += input;
            }

            private void Four_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "4";
                this.textBox1.Text += input;
            }

            private void Five_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "5";
                this.textBox1.Text += input;
            }

            private void Six_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "6";
                this.textBox1.Text += input;
            }

            private void Seven_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "7";
                this.textBox1.Text += input;
            }

            private void Eight_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "8";
                this.textBox1.Text += input;
            }

            private void Nine_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += "9";
                this.textBox1.Text += input;
            }

            private void Decimal_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                input += ".";
                this.textBox1.Text += input;
            }

            private void Divide_Click(object sender, EventArgs e)
            {
                operand1 = input;
                operation = '/';
                input = string.Empty;
            }

            private void Multiply_Click(object sender, EventArgs e)
            {
                operand1 = input;
                operation = '*';
                input = string.Empty;
            }

            private void Plus_Click(object sender, EventArgs e)
            {
                operand1 = input;
                operation = '+';
                input = string.Empty;
            }

            private void Minus_Click(object sender, EventArgs e)
            {
                operand1 = input;
                operation = '-';
                input = string.Empty;
            }

            private void Clear_Click(object sender, EventArgs e)
            {
                this.textBox1.Text = "";
                this.input = string.Empty;
                this.operand1 = string.Empty;
                this.operand2 = string.Empty;
            }

            private void Equal_Click(object sender, EventArgs e)
            {
                operand2 = input;
                double.TryParse(operand1, out double num1);
                double.TryParse(operand2, out double num2);
                this.textBox1.Text = "";
                this.input = string.Empty;
                this.operand1 = string.Empty;
                this.operand2 = string.Empty;
                
                if (operation == '/')
                {
                    result = num1 / num2;
                    textBox1.Text = result.ToString();
                }
                else if (operation == '*')
                {
                    result = num1 * num2;
                    textBox1.Text = result.ToString();
                }
                else if (operation == '+')
                {
                    result = num1 + num2;
                    textBox1.Text = result.ToString();
                }
                else if (operation == '-')
                {
                    if (num2 != 0)
                    {
                        result = num1 / num2;
                        textBox1.Text = result.ToString();
                    }
                    else
                    {
                        textBox1.Text = "ERROR: cannot divide by 0";
                    }
                }
            }
        }
    }


    Conner Ferguson


    • Edited by cpdf20 Friday, January 19, 2018 10:01 PM
    Friday, January 19, 2018 6:11 PM

All replies

  • Maybe you should execute ‘input = result.ToString()’ after obtaining the result in Equal_Click.

    Friday, January 19, 2018 6:50 PM
  • Where would that be put and what would that look like in code?


    Conner Ferguson

    Friday, January 19, 2018 9:21 PM
  • This is obviously homework, so we're not going to write the code for you.  However, Viorel's response correctly answered BOTH of your questions, before you asked them.  Perhaps you need to read through it again.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Friday, January 19, 2018 9:36 PM
  • Hi cpdf98,

    As Viorel said, assign result to variable named input in your Equal_Click method. like this:

    private void Equal_Click(object sender, EventArgs e)
            {
                operand2 = input;
                double.TryParse(operand1, out double num1);
                double.TryParse(operand2, out double num2);
                this.textBox1.Text = "";
                this.input = string.Empty;
                this.operand1 = string.Empty;
                this.operand2 = string.Empty;
    
                if (operation == '/')
                {
                    result = num1 / num2;
                    textBox1.Text = result.ToString();
                }
                else if (operation == '*')
                {
                    result = num1 * num2;
                    textBox1.Text = result.ToString();
                }
                else if (operation == '+')
                {
                    result = num1 + num2;
                    textBox1.Text = result.ToString();
                }
                else if (operation == '-')
                {
                    if (num2 != 0)
                    {
                        result = num1 / num2;
                        textBox1.Text = result.ToString();
                    }
                    else
                    {
                        textBox1.Text = "ERROR: cannot divide by 0";
                    }
                }
    
                input = result.ToString();
            }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 23, 2018 5:40 AM
    Moderator