Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello codeproject

My problem is that i currently making a calculator in c#, and this is some of the problems i have:

It crashes when i divide by 0.

When i wanna make it show the whole calculation, it will reverse the signs used
(plus = minus, minus = plus and so on)


How can i solve them??
I hope you will understand my problem, and try help me.
Sorry if my english is bad.
PS some of it is in Danish so just ignore the comments after //
Thanks for the help

C#

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 CalForm : Form {
        bool plus = false;
        bool minus = false;
        bool multiply = false;
        bool divide = false;
        bool equal = false;
        public CalForm() {
            InitializeComponent();
            Text = "Calculator";
        }

        private void CalForm_Load(object sender, EventArgs e) {
        }
        // 1
        private void button1_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "1";
        }
        // 2
        private void button2_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "2";
        }
        // 3
        private void button3_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "3";
        }
        // 4
        private void button4_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "4";
        }
        // 5
        private void button5_Click(object sender, EventArgs e) {
            if (equal)
            {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "5";
        }
        // 6
        private void button6_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "6";
        }
        // 7
        private void button7_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "7";
        }
        // 8
        private void button8_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "8";
        }
        // 9
        private void button9_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "9";
        }
        // 0
        private void button14_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "0";
        }
        // ,
        private void button15_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            if (textBox1.Text.Contains(",")) {
                return;
            }
            else {
                textBox1.Text = textBox1.Text + ",";
            }
        }
        //Dette er plus
        private void button11_Click(object sender, EventArgs e) {
            if (textBox1.Text == "") {
                return;
            }
            else {
                plus = true;
                textBox1.Tag = textBox1.Text;
                textBox1.Text = "+";
            }
        }
        //Dette er "lig med" for regne reglerne i lommeregneren
        private void button13_Click(object sender, EventArgs e) {
            equal = true;
            if (plus) {
                decimal deo = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text);
                textBox1.Text = deo.ToString();
            }
            if (minus) {
                decimal deo = Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text);
                textBox1.Text = deo.ToString();
            }
            if (multiply) {
                decimal deo = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text);
                textBox1.Text = deo.ToString();
            }
            if (divide) {
                decimal deo = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text);
                textBox1.Text = deo.ToString();
            }
        }
        //Dette er minus
        private void button12_Click(object sender, EventArgs e) {
            if (textBox1.Text == "") {
                return;
            }
            else {
                minus = true;
                textBox1.Tag = textBox1.Text;
                textBox1.Text = "-";
        }
    }
        //Dette er gange
        private void button16_Click(object sender, EventArgs e) {
            if (textBox1.Text == "") {
                return;
            }
            else {
                multiply = true;
                textBox1.Tag = textBox1.Text;
                textBox1.Text = "";
            }
        }
        //Dette er clear
        private void button17_Click(object sender, EventArgs e) {
            plus = minus = multiply = divide = false;
            textBox1.Tag = textBox1.Text;
            textBox1.Text = "";
        }
        // Dette er divider
        private void button10_Click(object sender, EventArgs e) {
            if (textBox1.Text == "") {
                return;
            }
            else {
                divide = true;
                textBox1.Tag = textBox1.Text;
                textBox1.Text = "";
            }
        }
        //Dette er pi
        private void button19_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "3,14";
        }
        //Dette er tau
        private void button21_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "6,28";
        }
        //Dette er e
        private void button20_Click(object sender, EventArgs e) {
            if (equal) {
                textBox1.Text = "";
                equal = false;
            }
            textBox1.Text = textBox1.Text + "2,71";
            // Dette er så man ikke kan dividere med 0
            
            }
        }
    }
}
Posted

divide by zero:
C#
private void button13_Click(object sender, EventArgs e) {
            equal = true;
            if (plus) {
                decimal deo = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text);
                textBox1.Text = deo.ToString();
            }
            if (minus) {
                decimal deo = Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text);
                textBox1.Text = deo.ToString();
            }
            if (multiply) {
                decimal deo = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text);
                textBox1.Text = deo.ToString();
            }
            if (divide) {
                try
                {
                    decimal deo = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text);
                    textBox1.Text = deo.ToString();
                }
                catch (DivideByZeroException)
                {
                  MessageBox.Show ("Cannot Divide by zero!")
                }
            }
        }


and the second problem is much more complicated... You have operators type boolean, so when you declare Plus_Click, Minus_Click and so on, you have to set the others to false.

Example:
C#
//Dette er plus
        private void button11_Click(object sender, EventArgs e) {
            if (textBox1.Text == "") {
                return;
            }
            else {
                plus = true;
                minus = false;
                multiply = false;
                divide = false;
                equal = false;
                textBox1.Tag = textBox1.Text;
                textBox1.Text = "+";
            }
        }


In this code I can't see any more errors, maybe it could be in different class.

I hope this helps.

-Pepin z Hane
 
Share this answer
 
v2
Comments
Nelek 10-Nov-12 18:46pm    
OP's commet to you moved from non-solution below
WOW it worked!!
Thank you so much ^^
Pepin z Hane 12-Nov-12 10:27am    
for little... so accept the solution.
just put that on try catch block
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900