4
Reply

Calculator problem

Josip Pavic

Josip Pavic

Nov 11 2011 3:48 AM
1.5k
I am absolute beginner in c#.
One of my firs lessons were making a simple calculator, and i am having problem with some code in it.
I think problem is in SWITCH loop.


namespace Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

double total1 = 0;
double total2 = 0;
private void btnOne_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnOne.Text;
}

private void btnTwo_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
}

private void btnThre_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnThre.Text;
}

private void btnFour_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnFour.Text;
}

private void btnFive_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnFive.Text;
}

private void btnSix_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnSix.Text;
}

private void btnSeven_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnSeven.Text;
}

private void btnEight_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnEight.Text;
}

private void btnNine_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnNine.Text;
}

private void btnZero_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnZero.Text;
}

private void btnClear_Click(object sender, EventArgs e)
{
txtDisplay.Clear();
}
string theOperator;
private void btnPlus_Click(object sender, EventArgs e)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();

theOperator = "+";


}

private void btnEquals_Click(object sender, EventArgs e)
{
switch (theOperator)
{
case "+":

total1 = total1 + double.Parse(txtDisplay.Text);
break;

case "-":
total1 = total1 - double.Parse(txtDisplay.Text);
break;
case "*":
total1 = total1 * double.Parse(txtDisplay.Text);
break;
case "/":
total1 = total1 / double.Parse(txtDisplay.Text);
break;
}





txtDisplay.Text = total2.ToString();
total1 = 0;
}

private void btnPoint_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnPoint.Text;
theOperator = ",";
}


private void btnMinus_Click(object sender, EventArgs e)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();

theOperator = "-";
}

private void button2_Click(object sender, EventArgs e)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();

theOperator = "*";
}

private void divideButton_Click(object sender, EventArgs e)
{

total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();

theOperator = "/";
}
}
}


Thanks for help
Jonny111

Answers (4)