Click here to Skip to main content
15,888,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Coding for calculator equals isn't working... notsure why

What I have tried:

<pre>	private void button5_Click(object sender, System.EventArgs e)
		{
            //Equal sign

            n2 = decimal.Parse(textBox1.Text = textBox1.Text);
            
            if (fun == "+")
			 n1 = n1 + n2;

			else
			if (fun == "-") 
			 n1 = n1 - n2;

			else
				if (fun == "*") 
				n1 = n1 * n2;

			else
				if (fun == "/") 
				n1 = n1 / n2;
			textBox1.Text = n1.ToString();
		}
Posted
Updated 28-Jun-20 21:54pm

1. what does 'isnt working...notsure why' mean ? if you cant describe the problem adequately, how do you expect us to help ? (Use "Improve solution")

2. this code
if (fun == "+")
 n1 = n1 + n2;

else
if (fun == "-")
 n1 = n1 - n2;

else
    if (fun == "*")
    n1 = n1 * n2;

else
    if (fun == "/")
    n1 = n1 / n2;

one word .. "awful" .. may I suggest
switch(fun)
{
  case "+" : 
    n1 = n1 + n2;
    break;
  case "-" : 
    n1 = n1 - n2;
    break;
  case "*" : 
    n1 = n1 * n2;
    break;
  case "/" : 
    n1 = n1 / n2;
    break;
}
 
Share this answer
 
v2
Comments
Maciej Los 29-Jun-20 2:46am    
5ed!
What is this statement supposed to do?
C#
n2 = decimal.Parse(textBox1.Text = textBox1.Text);

Particularly the expression in parentheses.
 
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