### 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.

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include #include 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<>quantity; if (quantity > 0) {subtotal = quantity * price1; cout<<"The subtotal is \$"< 2) {subtotal = quantity * price2; cout<<"The subtotal is \$"< 20) {cout<<"Sorry, the jars are limited to 20 per customer."<
Last edited on
Else has a condition.
 ``123`` ``````else { cout<<"Sorry, you actually have to buy something."<
Last edited on
You can' t have a condition with the else.

 ``12`` ``````else (quantity < 0) {cout<<"Sorry, you actually have to buy something."<

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:
 ``12345678`` ``````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.
 ``12`` ``````else (quantity < 0) {cout<<"Sorry, you actually have to buy something."<

if should be
 ``12`` ``````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.