1

I'm supposed to be using nested if/else statements in my assignment. My IF statements execute just fine, but when I add "else" anywhere it comes up with a red squiggly line under it. Is it because I'm using the brackets wrong?

{
const int package = 99;
double quantity, total1, total2, total4;

cout << "How many units were sold?\n";
cin >> quantity;

if (quantity >= 1 && quantity <= 19);
{
total1 = package * quantity - (.2 * package * quantity);
cout << "Sale total: " << total1 << endl;
}

else if (quantity >= 20 && quantity <= 99);
{
total2 = package * quantity - (.3 * package * quantity);
cout << "Sale total: " << total2 << endl;
}

else (quantity >= 100);
{
total4 = package * quantity - (.5 * package * quantity);
cout << "Sale total: " << total4 << endl;
}

The errors I'm getting back from the lines using "else" are:

  • E0127 expected a statement
  • C2181 illegal else without matching if
2
  • 'else (quantity >= 100); should read else if (quantity >= 100) Commented Sep 23, 2019 at 2:21
  • 1
    I would propose that your if statements do not function just fine. Did you test the case where the condition is supposed to be false before you tried adding else clauses? Commented Sep 23, 2019 at 3:07

2 Answers 2

3

The problem is that you are putting semicolons at the end of your if statements, and before the opening bracket. The last else is missing an if, but I beleive that you don't event need a condition here at all. Also I see an opening bracket at the top without one at the end, but I assume that is due to bad copy/pasting the code. The correct code would look like that:

if (quantity >= 1 && quantity <= 19)
{
total1 = package * quantity - (.2 * package * quantity);
cout << "Sale total: " << total1 << endl;
}

else if (quantity >= 20 && quantity <= 99)
{
total2 = package * quantity - (.3 * package * quantity);
cout << "Sale total: " << total2 << endl;
}

else
{
total4 = package * quantity - (.5 * package * quantity);
cout << "Sale total: " << total4 << endl;
}
Sign up to request clarification or add additional context in comments.

3 Comments

What you say about the else not needing a test is only true the the user enters a positive integral value. However, the variable quantity is of type double The else will therefore catch values less than 1.0, between 19.0 and 20.0` (not inclusive). There is nothing preventing the user entering such values. Admittedly, coding errors aside, that's true of the OP's code, but removing the last test for quantity >= 100 does mean more values pass into the else.
@Peter What you said is correct, but I don't think it's really relevant here. OP is obviously just starting out with a simple program, and for now he probably shouldn't worry about that. You could also argue that a quantity is never supposed to be negative. However that is a valid point to take into consideration for improvement further down the line.
Thank you! That was it!
1

You're not supposed to put a semicolon after the conditions of ifs. Also, it doesn't make sense to have a condition on an else that's not an else if.

4 Comments

Might also be worth noting the reason that the first block runs (semicolon terminates the conditional, block statement is entered normally)
@HotelCalifornia That doesn't happen in this case, because the dangling elses cause compile errors that prevent it from getting that far.
per the OP: "My IF statements execute just fine, but when I add "else" anywhere it comes up with a red squiggly line"
@HotelCalifornia Ah, you're right. I missed that and was just looking at the code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.