Reputation: 1
Why does this not work?
private void btnEquals_Click(object sender, EventArgs e)
{
if (plusButtonClicked == true)
{
total2 = total1 + double.Parse(txtDisplay.Text);
}
else if (minusButtonClicked == true);
{
total2 = total1 - double.Parse(txtDisplay.Text);
}
else if (multiplyButtonClicked == true);
{
total2 = total1 * double.Parse(txtDisplay.Text);
}
else
{
total2 = total1 / double.Parse(txtDisplay.Text);
}
It works until the second "else if" it says invalid expression else
Upvotes: 0
Views: 3684
Reputation: 4533
Meaning of semicolon after if-else menace you just stop the condition.
Remove ;
and add:
private void btnEquals_Click(object sender, EventArgs e)
{
if (plusButtonClicked == true)
{
total2 = total1 + double.Parse(txtDisplay.Text);
}
else if (minusButtonClicked == true)
{
total2 = total1 - double.Parse(txtDisplay.Text);
}
else if (multiplyButtonClicked == true)
{
total2 = total1 * double.Parse(txtDisplay.Text);
}
else
{
total2 = total1 / double.Parse(txtDisplay.Text);
}
}
Upvotes: 0
Reputation: 15344
remove semicolon after else-if
else if (minusButtonClicked == true); //remove semicolon
also no need to use ==true
in (minusButtonClicked == true)
Upvotes: 1
Reputation: 2082
private void btnEquals_Click(object sender, EventArgs e) { if (plusButtonClicked == true) { total2 = total1 + double.Parse(txtDisplay.Text); } else if (minusButtonClicked == true) { total2 = total1 - double.Parse(txtDisplay.Text); } else if (multiplyButtonClicked == true) { total2 = total1 * double.Parse(txtDisplay.Text); } else { total2 = total1 / double.Parse(txtDisplay.Text); } }
Upvotes: 0
Reputation: 82893
Remove semi-colons after the else if's
private void btnEquals_Click(object sender, EventArgs e)
{
if (plusButtonClicked == true)
{
total2 = total1 + double.Parse(txtDisplay.Text);
}
else if (minusButtonClicked == true)
{
total2 = total1 - double.Parse(txtDisplay.Text);
}
else if (multiplyButtonClicked == true)
{
total2 = total1 * double.Parse(txtDisplay.Text);
}
else
{
total2 = total1 / double.Parse(txtDisplay.Text);
}
}
Upvotes: 1
Reputation: 45222
Because you put a semicolon after the first else-if line.
private void btnEquals_Click(object sender, EventArgs e)
{
if (plusButtonClicked == true)
{
total2 = total1 + double.Parse(txtDisplay.Text);
}
else if (minusButtonClicked == true); // <<== There should not be a semicolon here
{
total2 = total1 - double.Parse(txtDisplay.Text);
}
else if (multiplyButtonClicked == true); // <<== or here
{
total2 = total1 * double.Parse(txtDisplay.Text);
}
else
{
total2 = total1 / double.Parse(txtDisplay.Text);
}
Upvotes: 5