I need a form that accepts two operands and an operator from the user and then performs the requested operation.
Specifications

Before performing the calculation, the application should check that both operands are numeric and that the operator is one of the following:

  • add

  • subtract

  • multiply

/ divide

It should also check that both operands are between 0 and 1,000,000 (non-inclusive). To perform this data validation, code IsPresent, IsDecimal, and IsValidData methods as described in chapter 7 of the book. In addition, code an IsOperator method that checks for a valid operator.

Code a private method named Calculate that performs the requested operation and returns a decimal value. This method should accept the following arguments:

Argument Description

decimal operand1 The value entered for the first operand. string operator1 One of these four operators: +, -, *, or /. decimal operand2 The value entered for the second operand.

The result of all operations should be rounded to four decimal places.

Heres what I have now:

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 Simple_Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // input will be converted into strings to display in the textbox
        private void btncalculate_Click(object sender, EventArgs e)
        {
           try
           {
                if (IsValidData())

                {
                    decimal Operand1 = Convert.ToDecimal(txtNumber1.Text);
                    decimal Operand2 = Convert.ToDecimal(txtNumber2.Text);
                    String Operator1 = Convert.ToString(txtOperator.Text);


                    decimal result = Calculation(Operand1 , Operand2, Operator1); 

                    txtResult.Text = result.ToString("f4");
                    txtNumber1.Focus();

                }
           }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.Message + "\n\n" +
                ex.GetType().ToString() + "\n" +
                ex.StackTrace, "Exception");
            }
}
        public bool IsValidData()
        {
            return IsPresent(txtNumber1, "Operand 1") &&
                   IsDecimal(txtNumber1, "Operand 1") &&
                   IsWithinRange(txtNumber1, "Operand 1", 0, 1000000) &&

                   IsPresent(txtNumber2, "Operand 2") &&
                   IsDecimal(txtNumber2, "Operand 2") &&
                   IsWithinRange(txtNumber2, "Operand 2", 0, 1000000) &&

                   IsOperator(txtNumber1, "Operator 1") &&
                   IsString(txtNumber1, "Operator 1") &&
                   IsWithinRange(txtOperator, "Operator", "+", "-", "*", "/");

        }



        public bool IsPresent(TextBox textBox, string name)
        {
            if (textBox.Text == "")
            {
                MessageBox.Show(name + " is a required field.", "Entry Error");
                textBox.Focus();
                return false;
            }
            return true;
        }

        public bool IsDecimal(TextBox textBox, string name)
        {
            try
            {
                Convert.ToDecimal(textBox.Text);
                return true;
            }
            catch (FormatException)
            {
                MessageBox.Show(name + " must be a decimal value.", "Entry Error");
                textBox.Focus();
                return false;
            }
        }

        public bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max)
        {
            decimal number = Convert.ToDecimal(textBox.Text);
            if (number < min || number > max)
            {
                MessageBox.Show(name + " must be between " + min + " and " + max + ".", "Entry Error");
                textBox.Focus();
                return false;
            }
            return true;
        }

        public bool IsOperator(TextBox textBox, string name)
        {
            try
            {
                Convert.ToString(textBox.Text);
                return true;
            }
            catch (FormatException)
            {
                MessageBox.Show(name + " must be a string.", "Entry Error");
                textBox.Focus();
                return false;
            }


        }



// Calculation method showing possible out comes of result

        private decimal Calculation(decimal Operand1, decimal Operand2, string Operator1)
        {
            decimal Result = 0;

            switch (Operator1)
            {
                case "+":
                    Result = Operand1 + Operand2;
                    break;

                case "-":
                    Result = Operand1 - Operand2;
                    break;

                case "*":
                    Result = Operand1 * Operand2;
                    break;

                case "/":
                    Result = Operand1 / Operand2;
                    break;
            }
        }        


        //close the form

        private void btnexit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //Clearing the textbox; 

        private void txtNumber1_TextChanged(object sender, EventArgs e)
        {
            txtOperator.Text = " ";
            txtNumber2.Text = " ";
            txtResult.Text = " ";
        }

        private void txtOperator_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtNumber2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}    

Here are my errors:

Error 1 The name 'IsString' does not exist in the current context
Error 2 No overload for method 'IsWithinRange' takes 6 arguments
Error 3 'Simple_Calculator.Form1.Calculation(decimal, decimal, string)': not all code paths return a value

Any help is greatly appreciated.
Thanks in advance.

Error 1 - You don't have a method name IsString

Error 2 - In line 58,IsWithinRange(txtOperator, "Operator", "+", "-", "*", "/");, you're trying to send a bunch of strings to the method when it expects a TextBox, a string and 2 decimal's.

Error 3 - Simple_Calculator.Form1.Calculation(decimal, decimal, string), has no return statment, but the declaration, private decimal Calculation(decimal Operand1, decimal Operand2, string Operator1) says it is expected to return a decimal.

Edited 2 Years Ago by tinstaafl

  1. Error--> I do not see a method called IsString
    2- Error--> Method IsWithinRange accepts only four arguments so you have to assign 4 arguments once you call your method, not six as you did it.
    3- Error--> if you have a method that is NOT void then you have to specify the return keyword! so I suggest to put some return keywords inside your Calculation method.

thanks but these are things that I already knew. What should the syntax be to correct the error?

I'm sure You will figure it out with in depth given explanation. Have an attempt and then if you are still not sure and if you still get errors, come back with the piece of code instead of entire class and help will be surely given to you.

Basically follow the Intellisense directions that pop up when you're typing the code. For instance, when it indicates that it is expecting a decimal that's what you're supposed to give it. If you need to pass something different, then you either need a different method, or an overload of an existing one. Or when it says it is expecting a return value that's what you have to give it. Like in your IsOperator method, the return type is bool and your return statements return true or false, which are boolean values.

Edited 2 Years Ago by tinstaafl

Find your IsOperator method a bit bizarre.
You're just checking if the textbox contains a string.
Well, believe me textboxes are designed to contain strings.
Check if the strings are "+","-","*" and "/" instead.

This question has already been answered. Start a new discussion instead.