-1

i'm trying to create a calculator

After i rund the code... if i do a calculation like 9 x 9 i get result of 81

but when i try to use the result of 81 and divide by any number i get error System.ArgumentOutOfRangeException

9 x 9 = 81 then straigh / by any number i get error System.ArgumentOutOfRangeException

but

if i do type 81 and / by 9 i get correct answer of 9

the error comes after i do the first calculation...when i try to execute other calculation using the result i get that error

i tried to execute other calculation using the result of the first calculation

the code i try to execute is down below

namespace FormCalculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(textBox.Text);
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            previousOperation = Operation.None;
            textBox.Clear();
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            if(textBox.Text.Length > 0)
            {
                double d;
                if (!double.TryParse(textBox.Text[textBox.Text.Length - 1].ToString(), out d))
                {
                    previousOperation = Operation.None;
                }

                textBox.Text = textBox.Text.Remove(textBox.Text.Length - 1, 1);
            }           
        }

        private void buttonDiv_Click(object sender, EventArgs e)
        {
            if (previousOperation != Operation.None)               
                PerformCalculation(previousOperation);

            previousOperation = Operation.Div;
            textBox.Text += (sender as Button).Text;
        }

        private void buttonMult_Click(object sender, EventArgs e)
        {
            if (previousOperation != Operation.None)          
                PerformCalculation(previousOperation);

            previousOperation = Operation.Mul;
            textBox.Text += (sender as Button).Text;
        }

        private void buttonSub_Click(object sender, EventArgs e)
        {
            if (previousOperation != Operation.None)             
                PerformCalculation(previousOperation);

            previousOperation = Operation.Sub;
            textBox.Text += (sender as Button).Text;
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (previousOperation != Operation.None)
                PerformCalculation(previousOperation);
                
            previousOperation = Operation.Add;                       
            textBox.Text += (sender as Button).Text;           
        }

        private void PerformCalculation(Operation previousOperation)
        {
            List<double> listNumbers = new List<double>();
            switch (previousOperation)
            {
                case Operation.Add:
                    listNumbers = textBox.Text.Split('+').Select(double.Parse).ToList();
                    textBox.Text = (listNumbers[0] + listNumbers[1]).ToString();
                    break;
                case Operation.Sub:
                    listNumbers = textBox.Text.Split('-').Select(double.Parse).ToList();
                    textBox.Text = (listNumbers[0] - listNumbers[1]).ToString();
                    break;
                case Operation.Mul:
                    listNumbers = textBox.Text.Split('*').Select(double.Parse).ToList();
                    textBox.Text = (listNumbers[0] * listNumbers[1]).ToString();
                    break;
                case Operation.Div:

                    try
                    {
                        listNumbers = textBox.Text.Split('/').Select(double.Parse).ToList();
                        textBox.Text = (listNumbers[0] / listNumbers[1]).ToString();
                        break;
                    }
                    catch (DivideByZeroException)
                    {
                        textBox.Text = "kkkkkkkkk";
                        throw;
                    }
                case Operation.None:
                    break;
                default:
                    break;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox.Text += (sender as Button).Text;
        }

        enum Operation
        {
            Add,
            Sub,
            Mul,
            Div,
            None

        }
        static Operation previousOperation = Operation.None;

        private void buttonResult_Click(object sender, EventArgs e)
        {
            if (previousOperation == Operation.None)
                return;
            else
                PerformCalculation(previousOperation);
            
        }
    }
}
Paulo
  • 1
  • 1
  • This isn't a debugging service. What line of code is causing the exception? That should tell you where the problem is. – John V Mar 26 '22 at 03:07

1 Answers1

0

I cannot recreate your issue with your code, but there are a few things you can improve that might help you find your issue when you're done.

Firstly, you do not need to catch DivideByZeroException with floating-point arithmetic. It will return infinity and will never throw an exception.

Secondly, if you wish to inform the user of an invalid math operation as a result of using a divide-by-zero operation you can change that code:

case Operation.Div:
    listNumbers = textBox.Text.Split('/').Select(double.Parse).ToList();
    string answer = listNumbers[1] != 0 ? 
        (listNumbers[0] / listNumbers[1]).ToString() : "Divide by zero";
    textBox.Text = answer;
    break;
ExplodatedFaces
  • 348
  • 2
  • 8
  • still getting the same error .... if i do 8 x 8 = 64, then if I just click on any symbol for calculation ( - + * / ) after execute the first calculation i get error the same error – Paulo Mar 26 '22 at 22:53
  • Is it giving you a line number for the error at all? Try adding a try/catch and catch the exception with the code: ``` catch (ArgumentOutOfRange e) { MessageBox.Show(e.ToString()); } ``` the last part in the message box, the very end will show the line number. – ExplodatedFaces Mar 28 '22 at 01:13