Error message help on basic calculator code

last snippet of code is giving me an error and I don't understand

Page 1 of 1

14 Replies - 1920 Views - Last Post: 08 June 2010 - 11:14 AM Rate Topic: -----

#1 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Error message help on basic calculator code

Posted 08 June 2010 - 09:42 AM

I am working on this code for a project, the only error message I am getting is "Error 1 Expected class, delegate, enum, interface, or struct" this is in referene to the btn_Click at the very bottom, this was done in visual studio, help, what did I do wrong?
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 Project_7_1
{
    public partial class FrmSimpleCalculator : Form
    {
        public FrmSimpleCalculator()
        {
            InitializeComponent();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                if(IsValidData())
                {
                    decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
                    string operator1 = Convert.ToString(txtOperator1.Text);
                    decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
                    decimal result = Calculate(operand1, operator1, operand2);
                    
                    txtResult.Text = result.ToString(Math.Pow, 4);
                    txtOperand1.Focus();
                     }
                    }
                    catch(Exception ex)
                     {
                        MessageBox.Show(ex.Message = "\n\n" + ex.GetType().ToString() + "\n" + ex.StackTrace, "Exception");
}
}
        public bool IsValidData()
        {
            return
                IsPresent(txtOperand1, "Operand 1") &&
                IsDecimal(txtOperand1, "Operand 1") &&
                IsWithinRange(txtOperand1, "Operand 1", 0, 1000000) &&

                IsPresent(txtOperator1, "Operator 1")&&
                IsOperator(txtOperator1, "Operator 1", "+", "-", "*", "/")&&

                IsPresent(txtOperand2, "Operand 2") &&
                IsDecimal(txtOperand2, "Operand 2") &&
                IsWithinRange(txtOperand2, "Operand 2", 0, 1000000);
        }

        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 IsOperator(TextBox textBox, string name)
        {
            try
            {
                Convert.ToString(textBox.Text);
                return true;
            }
            catch(FormatException)
            {
                MessageBox.Show(name + " must be an operator.", "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;
        }

        private decimal Calculate(decimal operand1, string operator1, decimal operand2)
        {
            if (operator1 == "+")
            {
                result = operand1 + operand2;
            }
            else if (operator1 == "-")
            {
                result = operand1 - operand2;
            }
            else if (operator1 == "*")
            {
                result = operand1 * operand2;
            }
            else
            {
                result = operand1 / operand2;
            }
            return result;
        }
          


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



Is This A Good Question/Topic? 0
  • +

Replies To: Error message help on basic calculator code

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5625
  • View blog
  • Posts: 12,064
  • Joined: 02-June 10

Re: Error message help on basic calculator code

Posted 08 June 2010 - 09:48 AM

You have an extra right brace just above the btnExit method.
That is closing your class/namespace. So the btnExit code it outside of your class.
Was This Post Helpful? 1
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Error message help on basic calculator code

Posted 08 June 2010 - 09:51 AM

You have too many closing braces.

This post has been edited by eclipsed4utoo: 08 June 2010 - 09:52 AM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Error message help on basic calculator code

Posted 08 June 2010 - 09:52 AM

You had an extra (stray) closing brace towards the end of your code. Here's your updated code

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

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                if(IsValidDate())
                {
                    decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
                    string operator1 = Convert.ToString(txtOperator1.Text);
                    decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
                    decimal result = Calculate(operand1, operator1, operand2);
                    
                    txtResult.Text = result.ToString(Math.Pow, 4);
                    txtOperand1.Focus();
                }
            }
            catch(Exception ex)
            {
                 MessageBox.Show(ex.Message = "\n\n" + ex.GetType().ToString() + "\n" + ex.StackTrace, "Exception");
            }
        }
    
        public bool IsValidData()
        {
            return
                IsPresent(txtOperand1, "Operand 1") &&
                IsDecimal(txtOperand1, "Operand 1") &&
                IsWithinRange(txtOperand1, "Operand 1", 0, 1000000) &&

                IsPresent(txtOperator1, "Operator 1")&&
                IsOperator(txtOperator1, "Operator 1", "+", "-", "*", "/")&&

                IsPresent(txtOperand2, "Operand 2") &&
                IsDecimal(txtOperand2, "Operand 2") &&
                IsWithinRange(txtOperand2, "Operand 2", 0, 1000000);
        }

        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 IsOperator(TextBox textBox, string name)
        {
            try
            {
                Convert.ToString(textBox.Text);
                return true;
            }
            catch(FormatException)
            {
                MessageBox.Show(name + " must be an operator.", "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;
        }

        private decimal Calculate(decimal operand1, string operator1, decimal operand2)
        {
            if (operator1 == "+")
            {
                result = operand1 + operand2;
            }
            else if (operator1 == "-")
            {
                result = operand1 - operand2;
            }
            else if (operator1 == "*")
            {
                result = operand1 * operand2;
            }
            else
            {
                result = operand1 / operand2;
            }
            return result;
        }
      
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}



Was This Post Helpful? 0
  • +
  • -

#5 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Error message help on basic calculator code

Posted 08 June 2010 - 10:01 AM

I am really confused, I have gone seriously wrong somewhere, now that I fixed the braces, the code says that I have 14 errors.....
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5625
  • View blog
  • Posts: 12,064
  • Joined: 02-June 10

Re: Error message help on basic calculator code

Posted 08 June 2010 - 10:07 AM

Then you didn't fix them - you added, deleted, too many or the wrong one(s).
Keep in mind that VS will highlight the matching brace for you, when you are next to one.
You just need to carefully match them up again.
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Error message help on basic calculator code

Posted 08 June 2010 - 10:09 AM

Well let us know what the errors are and we'll try to help resolve them (You have to tell us as we cannot see your screen, well some of can but we don't talk about it in public :P)
Was This Post Helpful? 0
  • +
  • -

#8 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Error message help on basic calculator code

Posted 08 June 2010 - 10:10 AM

What are the errors? would help letting us know so that we can help you fix them.
Was This Post Helpful? 0
  • +
  • -

#9 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Error message help on basic calculator code

Posted 08 June 2010 - 10:14 AM

These are the error messages I am now getting

Error 1 No overload for method 'IsOperator' takes '6' arguments

Error 2 Cannot implicitly convert type 'string' to 'decimal'

code reflects changes
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 Project_7_1
{
    public partial class FrmSimpleCalculator : Form
    {
        public FrmSimpleCalculator()
        {
            InitializeComponent();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsValidData())
                {
                    decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
                    string operator1 = Convert.ToString(txtOperator1.Text);
                    decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
                    decimal result = Calculate(operand1, operator1, operand2);

                    txtResult.Text = result.ToString("f4");
                    txtOperand1.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.GetType().ToString() + "\n" + ex.StackTrace, "Exception");
            }
        }

        public bool IsValidData()
        {
            return
                IsPresent(txtOperand1, "Operand 1") &&
                IsDecimal(txtOperand1, "Operand 1") &&
                IsWithinRange(txtOperand1, "Operand 1", 0, 1000000) &&

                IsPresent(txtOperator1, "Operator 1") &&
                IsOperator(txtOperator1, "Operator 1", "+", "-", "*", "/") &&

                IsPresent(txtOperand2, "Operand 2") &&
                IsDecimal(txtOperand2, "Operand 2") &&
                IsWithinRange(txtOperand2, "Operand 2", 0, 1000000);
        }

        public bool IsPresent(TextBox textBox, string name)
        {
            if (txtOperand1.Text == "" && txtOperand2.Text == "")
            {
                MessageBox.Show(name + " is a required field.", "Entry Error");
                txtOperand1.Focus();
                txtOperand2.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 IsOperator(TextBox textBox, string name)
        {
            try
            {
                Convert.ToString(textBox.Text);
                return true;
            }
            catch (FormatException)
            {
                MessageBox.Show(name + " must be an operator.", "Entry Error");
                txtOperator1.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;
        }

        private decimal Calculate(decimal operand1, string operator1, decimal operand2)
        {
            if (operator1 == "+")
            {
                decimal result = operand1 + operand2;
            }
            else if (operator1 == "-")
            {
                decimal result = operand1 - operand2;
            }
            else if (operator1 == "*")
            {
                decimal result = operand1 * operand2;
            }
            else
            {
                decimal result = operand1 / operand2;
            }
            return result;
        }

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



Never mind! I got it! Thank you everyone for your help, this one was driving me crazy
Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Error message help on basic calculator code

Posted 08 June 2010 - 10:18 AM

Error #1:

If you look at where you're using IsOperator (in IsValidDate) you're passing it 6 parameters

IsOperator(txtOperator1, "Operator 1", "+", "-", "*", "/")



But if you look at the definition for IsOperator it only expects 2:
  • TextBoc textBox
  • string name


public bool IsOperator(TextBox textBox, string name)



So when calling it you only pass 2 parameters (or Visual Studio will puke on you, like it currently is). For error #2 what line is it occurring on?
Was This Post Helpful? 0
  • +
  • -

#11 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Error message help on basic calculator code

Posted 08 June 2010 - 10:25 AM

I am no longer getting errors, and the simple calculator is calculating properly, but it doesn't show a Error Entry Message box if an operator is not entered into the Operator textBox,
In the IsPresent code I have put that the text boxes were blank, that it would display an error message, but I need it do put up an error message if the operator textBox is left blank or that it is invalid if something other than + - * / is entered.
Was This Post Helpful? 0
  • +
  • -

#12 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Error message help on basic calculator code

Posted 08 June 2010 - 10:31 AM

I know this is where the error is, but how do you put in actual operators as arguments?
 public bool IsValidData()
        {
            return
                IsPresent(txtOperand1, "Operand 1") &&
                IsDecimal(txtOperand1, "Operand 1") &&
                IsWithinRange(txtOperand1, "Operand 1", 0, 1000000) &&

                [color="#FF0000"]IsPresent(txtOperator1, "Operator 1") &&
                IsOperator(txtOperator1, "Operator 1") &&[/color]
                

                IsPresent(txtOperand2, "Operand 2") &&
                IsDecimal(txtOperand2, "Operand 2") &&
                IsWithinRange(txtOperand2, "Operand 2", 0, 1000000);
        }

        public bool IsPresent(TextBox textBox, string name)
        {
            if (txtOperand1.Text == "" && txtOperand2.Text == "")
                
            {
                MessageBox.Show(name + " is a required field.", "Entry Error");
                txtOperand1.Focus();
                txtOperand2.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 IsOperator(TextBox textBox, string name)
        {
            try
            {
                Convert.ToString(textBox.Text);
                return true;
            }
            catch (FormatException)
            {
                MessageBox.Show(name + " must be an operator.", "Entry Error");
                txtOperator1.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;
        }


Was This Post Helpful? 0
  • +
  • -

#13 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Error message help on basic calculator code

Posted 08 June 2010 - 10:48 AM

One option you have is to change your IsOperator to accept 2 parameters: the TextBox, and a string of valid operators. Then split your string of operators on the comma and check what they've provided. Here's an example

public bool IsOperator(TextBox textBox, string operators)
{
    try
    {
        foreach (string s in operators.Split(new char[] { ',' }))
        {
            if (textBox.Text.Trim() == s)
                return true;
            else
                throw new ArgumentException("The operator must be a valid operator: +, -, *, /", "name");
        }
        return true;
    }
    catch (ArgumentException ex)
    {
        MessageBox.Show(ex.Message);
        txtOperator1.Focus();
        return false;
    }
}



Then when you call it do like so

IsOperator(txtOperator1, "+,-,*,/")


Was This Post Helpful? 0
  • +
  • -

#14 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Error message help on basic calculator code

Posted 08 June 2010 - 11:00 AM

For the record I would change your IsDecimal method to use Decimal.TryParse, which will return true or false depending on whether a valid decimal value was provided. Something like this

public bool IsDecimal(TextBox textBox)
{
    try
    {
        decimal outValue;
        bool converted = Decimal.TryParse(textBox.Text, out outValue);

        if (converted)
            return true;
        else
            throw new FormatException("Invalid value. Input much be a decimal value");
    }
    catch (FormatException ex)
    {
        MessageBox.Show(ex.Message, "Entry Error");
        textBox.Focus();
        return false;
    }
}



Then where you call it in IsValidData change it to

IsDecimal(txtOperand1)


Was This Post Helpful? 0
  • +
  • -

#15 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Error message help on basic calculator code

Posted 08 June 2010 - 11:14 AM

If you're needing more information on creating a calculator you can check out Basic Calculator in C# that you may find useful :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1