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
Loading
Jean PaulPosted Nov 11, 2011, 4:35 AM
I created a form and placed +,-,= buttons and tested it. The calculation was not correctly displayed. I believe this is the problem. Anyway following are the fixes in the code:
1) No need of total2 variable
2) Display the total 1 on equal click (your code was showing total 2)
I had a suggestion that these type of code requires the form as attachment. Otherwise the solution will get delayed.
Anyway your logic is Good. Please find the corrected code. If any other problems exists let me know. (else mark as answer)
Regards,
Jean Paul
Jean PaulPosted Nov 11, 2011, 4:57 AM
Josip PavicPosted Nov 11, 2011, 4:43 AM
Satish BhatPosted Nov 11, 2011, 4:35 AM