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.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
int quantity;
double subtotal;
doubleconst 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;}
elseif (quantity > 2)
{subtotal = quantity * price2;
cout<<"The subtotal is $"<<subtotal<<endl;}
elseif (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;
}
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
}
elseif(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!";