have been working on this simple code for hours now

Ok, I have tried so many different things with this code. I would so much appreciate any help I can get here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

int main()
{
	//declare variables
	int amtOrdered   = 0;
	double price     = 0.00;
	double totalCost = 0.00;

    //get users input
	cout << "Please enter desired amount: ";
	cin >> amtOrdered;
    if (amtOrdered > 0 && amtOrdered <= 50)
	    totalCost = amtOrdered * .50;
	{
**	else if (amtOrdered >= 51 && amtOrdered <= 100)
	    totalCost = amtOrdered * .48
	else if (amtOrdered >= 100 && amtOrdered <= 499)
	    totalCost = amtOrdered * .45
	else if (amtOrdered >= 500);
	}
	//end ifs
	cout << "   pens were ordered " << amtOrdered << endl;
	cout << "at a price of " << totalCost << endl;
	cout << "each for a total of: " << totalCost << endl;
    return 0;
	
}

error C2181: illegal else without matching if
starting at line **
Thank you so much in advance.
get rid of the { at line 16 as well as the } on line 22. You have the else ifs enclosed in an if statement and they shouldn't be.

You also need to add ; at the end of lines 18 and twenty.

It should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main()
{
	//declare variables
	int amtOrdered   = 0;
	double price     = 0.00;
	double totalCost = 0.00;

    //get users input
	cout << "Please enter desired amount: ";
	cin >> amtOrdered;
    if (amtOrdered > 0 && amtOrdered <= 50)
	    totalCost = amtOrdered * .50;
	else if (amtOrdered >= 51 && amtOrdered <= 100)
	    totalCost = amtOrdered * .48;
	else if (amtOrdered >= 100 && amtOrdered <= 499)
	    totalCost = amtOrdered * .45;
	else if (amtOrdered >= 500);
	//end ifs
	cout << "   pens were ordered " << amtOrdered << endl;
	cout << "at a price of " << totalCost << endl;
	cout << "each for a total of: " << totalCost << endl;
    return 0;
	
}



Also to note, your last else if statement:
else if (amtOrdered >= 500);

isnt doing anything.
Last edited on
Thank you! You are awesome. I don't care what those other people say about you (JK).
It will compile and run now however I still get a warning saying:

empty controlled statement found; is this the intent?
This is at line 21. Do you know why?

Which is fine. The program runs but I want to be better at this stuff and the only way to do that is to learn from my mistakes.
The warning is about line 20:
else if (amtOrdered >= 500);
There is an if without any block, you can remove that line to avoid the warning
Great I knew you were OK
Thank you very much
Topic archived. No new replies allowed.