Reputation: 11
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:
Upvotes: 1
Views: 390
Reputation: 100
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;
}
Upvotes: 3
Reputation: 48572
You're not supposed to put a semicolon after the conditions of if
s. Also, it doesn't make sense to have a condition on an else
that's not an else if
.
Upvotes: 1