Solved

Calculator help

Posted on 2014-02-23
4
297 Views
Last Modified: 2014-02-23
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

0
Comment
Question by:ryanbecker24
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39881290
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().
0
 

Author Comment

by:ryanbecker24
ID: 39881303
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
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39881311
Just give your result variable an initial value:

    decimal result = 0.0;
0
 

Author Comment

by:ryanbecker24
ID: 39881425
Thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sum Column in GridView 3 42
Get id from json Data with NewtonSoft 3 32
Error on Add method 1 37
Web Form VB.Net  import CSV 4 21
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now