newbie trouble with if/else statements

Hi guys, first time post. Im working on what i figured an easy program to calculate the price of jars of honey. Prices will go down the more you guy, with 2 or less being $9 a jar, 3 to 10 being $8, and 11 to 20 being $7. Max you can buy is 20.

I haven't put all the if/else options in yet, because Im already getting errors, and im positive its in the if/else section.

Is it a simple syntax error? Im working ahead of the class and my prof wont help me yet.

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
30
31
32
33
34
35
36
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int quantity;
	double subtotal;
	double const price1 = 9.00, price2 = 8.00, price3 = 7.00, tax_rate = 0.095;

	cout<<setprecision(2)<<fixed;
	cout<<"Hi there. For whatever reason, I am a farmer who sells jars of honey on the side of the road, but has scripted a program to calulate the price."<<endl;
	cout<<"Prices are as follows, with discounts towards larger purchases:"<<endl;
	cout<<"1 jar: $"<<setw(13)<<"$"<<price1<<endl;
	cout<<"3 or more jars:"<<setw(6)<<"$"<<price2<<endl;
	cout<<"10 or more jars:"<<setw(5)<<"$"<<price3<<endl;
	cout<<" "<<endl;

	cout<<"Now, how many jars of honey would you like to purchase?"<<endl;
	cin>>quantity;

	if (quantity > 0)
		{subtotal = quantity * price1;
		cout<<"The subtotal is $"<<subtotal<<endl;}
	else if (quantity > 2)
		{subtotal = quantity * price2;
		cout<<"The subtotal is $"<<subtotal<<endl;}
	else if (quantity > 20)
		{cout<<"Sorry, the jars are limited to 20 per customer."<<endl;}
	else (quantity < 0)
		{cout<<"Sorry, you actually have to buy something."<<endl;}

	system ("pause");
	return 0;
}
Last edited on
Else has a condition.
1
2
3
else {
      cout<<"Sorry, you actually have to buy something."<<endl;
}
Last edited on
You can' t have a condition with the else.

1
2
else (quantity < 0)
		{cout<<"Sorry, you actually have to buy something."<<endl;}


consider making quantity unsigned.

set your indent tab to equal 4 spaces, and limit your lines of code to 80 chars - just press enter if it is too long, the compiler doesn't care.

Good luck !!
What errors are you getting? Are they debug errors or runtime errors? It helps explaining what specific error you are getting to help us narrow down the part of the code we need to look over.

EDIT: your way of organizing if and else if statements can get pretty unorganized, i would recommend formatting if's like so:
1
2
3
4
5
6
7
8
if(quantity>0)
{                                         //brackets help keep everything neat and tidy.
        //code here
}
else if(quantity>2)
{  
        //code here
}


EDIT2: Your else statements is either missing an if or you arent using it right.
1
2
else (quantity < 0)
		{cout<<"Sorry, you actually have to buy something."<<endl;}

if should be
1
2
else      //else input doesnt match any of the ifs
   cout << "Sorry!";
Last edited on
Here's his error, if you want to know.

Line 31: error: expected `;' before '{' token
Last edited on
i think, it's because fixed
Last edited on
Topic archived. No new replies allowed.