Link to home
Start Free TrialLog in
Avatar of ryanbecker24
ryanbecker24

asked on

Calculator help

I am getting errors on the line decimal result = Convert.ToDecimal(txtResult.Text);. It says input string is not in the correct format. I am new to C# so, any help would be appreciated. Thanks in advance.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
            decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
            decimal result = Convert.ToDecimal(txtResult.Text);
            
            string operator1 = txtOperator.Text;

            if (operator1 == "+")
            {
                result = operand1 + operand2;
                
            }

            else if (operator1 == "-")
	        {
		        result= operand1-operand2;
                
	        }
            else if (operator1 == "*")
	        {
                result = operand1 * operand2;
               
	        }
            else if (operator1 == "/")
            {
                result = operand1 / operand2;
             
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}

Open in new window

Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

The result is being calculated, so you should be outputting it, not reading it in from the form.

Change:
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
            decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
            decimal result = Convert.ToDecimal(txtResult.Text);
            
            string operator1 = txtOperator.Text;

            if (operator1 == "+")
            {
                result = operand1 + operand2;
                
            }

            else if (operator1 == "-")
	        {
		        result= operand1-operand2;
                
	        }
            else if (operator1 == "*")
	        {
                result = operand1 * operand2;
               
	        }
            else if (operator1 == "/")
            {
                result = operand1 / operand2;
             
            }

        }

Open in new window


To:
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
            decimal operand2 = Convert.ToDecimal(txtOperand2.Text);            
            string operator1 = txtOperator.Text;

            decimal result;
            if (operator1 == "+")
            {
                result = operand1 + operand2;
                
            }

            else if (operator1 == "-")
	        {
		        result= operand1-operand2;
                
	        }
            else if (operator1 == "*")
	        {
                result = operand1 * operand2;
               
	        }
            else if (operator1 == "/")
            {
                result = operand1 / operand2;
             
            }

            txtResult.Text = result.ToString();
        }

Open in new window


*That error will occur for "operand1" and "operand2" if the value in the TextBox cannot be converted to a proper decimal.  To prevent that type of error, use Decimal.TryParse().
Avatar of ryanbecker24
ryanbecker24

ASKER

Thanks, but txtResult.Text = result.ToString() isn't working. It is saying use of unassigned local variable, but its being used though. Also, I don't understand what you mean with the asterisk. Sorry, for all the questions
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you