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.

Recommended Answers

All 10 Replies

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.

  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.

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.

when i click calculate, it dosn't show anything

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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
            {

                decimal operand1 = Convert.ToDecimal(txtOperand1.Text);

                string operator1 = txtOperator.Text;

                decimal operand2 = Convert.ToDecimal(txtOperand2.Text);

                decimal result = Calculate(operand1, operator1, operand2);

                result = Math.Round(result, 4);
                this.txtResult.Text = result.ToString();
                txtOperand1.Focus();

            }

            // Handles All Exceptions

            catch (Exception ex)

            {

                MessageBox.Show(ex.ToString(), "Error Occured");

                decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
                string operator1 = txtOperator.Text;
                decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
                decimal result = Calculate(operand1, operator1, operand2);

                result = Math.Round(result, 4);
                this.txtResult.Text = result.ToString();
                txtOperand1.Focus();

            }

        }

        private decimal Calculate(decimal Operand1, string Operator1,

        decimal Operand2)

        {
            decimal result = 0;
            if (Operator1 == "+")
                result = Operand1 + Operand2;
            else if (Operator1 == "-")
                result = Operand1 - Operand2;
            else if (Operator1 == "*")
                result = Operand1 * Operand2;
            else if (Operator1 == "/")
                result = Operand1 / Operand2;
            return result;

        }

    public bool IsValidData()

        {

            return IsPresent(txtOperand1, "Operand1") &&

            IsDecimal(txtOperand1, "Operand1") &&

            IsWithinRange(txtOperand1, "Operand1", 0, 1000000) &&

            IsPresent(txtOperand2, "Operand2") &&

            IsDecimal(txtOperand2, "Operand2") &&

            IsWithinRange(txtOperand2, "Operand2", 0, 1000000) &&

            IsOperator(txtOperand1, "Operator1");

        }

        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)

        {
            decimal number = 0m;
            if (Decimal.TryParse(textBox.Text, out number))
            {

                Convert.ToDecimal(textBox.Text);

                return true;

            }

            else
            {

                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 within range

            if (number < min || number > max)

            {
                MessageBox.Show(name + " must be between " + min.ToString() + " and " + max.ToString() + ".", "Entry Error");

                textBox.Focus();

                return false;

            }

            return true;

        }

        public bool IsOperator(TextBox textBox, string name)

        {

            try

            {

                Convert.ToString(textBox.Text);

                return true;

            }

            catch (Exception)

            {

                MessageBox.Show(name + "Operator is not valid.", "Entry Error");

                textBox.Focus();

                return false;

            }

        }

        private void btnExit_Click(object sender, System.EventArgs e)

        {

            //Close

            this.Close();

        }

        private void ClearResult(object sender, System.EventArgs e)

        {

            this.txtResult.Text = "";

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

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

        private void btnCalculate_Click_1(object sender, EventArgs e)
        {

        }

        private void txtResult_TextChanged(object sender, EventArgs e)
        {

        }
    }

}

Your calculate button click event handler is doing nothing. So nothing happens.
@GULAR, read this

There is such a thing as too much whitespace.

commented: That comment could go horribly wrong in some forums. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.