I am trying to make a simple calculator. The boolean value in the bttn_Click() isn't working properly. The calculator is supposed to take in values and then reset when an operation is pressed. Instead, the calculator continues to append numbers to the value in the text box. As a result, the bttnEquals_Click() does not work either. I tried putting different values in the text box and determined that the the boolean is the culprit. Is there something wrong with the logic? TIA
C# file
public partial class WebForm1 : System.Web.UI.Page
{
Boolean op_pressed = false;
String operation = "";
Double result = 0.0;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void bttn_Click(object sender, EventArgs e)
{
if (op_pressed)
{
txtDisplay.Text = String.Empty;
}
Button b = (Button)sender;
txtDisplay.Text += b.Text;
}
protected void bttnClear_Click(object sender, EventArgs e)
{
txtDisplay.Text = string.Empty;
}
protected void bttnOperation_Click(object sender, EventArgs e)
{
op_pressed = true;
Button b = (Button)sender;
operation = b.Text;
result = Double.Parse(txtDisplay.Text);
}
protected void bttnEquals_Click(object sender, EventArgs e)
{
switch(operation)
{
case "+":
txtDisplay.Text = (result + Double.Parse(txtDisplay.Text)).ToString();
break;
case "-":
txtDisplay.Text = (result - Double.Parse(txtDisplay.Text)).ToString();
break;
case "*":
txtDisplay.Text = (result * Double.Parse(txtDisplay.Text)).ToString();
break;
case "/":
txtDisplay.Text = (result / Double.Parse(txtDisplay.Text)).ToString();
break;
default:
break;
}
op_pressed = false;
}
}
}