0

It wont compile!

I know something is probably wrong with my if condition but im unsure of what.

if (weight > 200)            
                shipping_cost = 49.95;
           else if (weight >= 100 & < 200)
                shipping_cost = 39.95;
           else if (weight >= 50 & < 100)
                shipping_cost = 24.95;
           else if (weight >= 20 & < 50)
                shipping_cost = 15.95;
           else 
                shipping_cost = 11.95;

Thanks

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by JHus00
Featured Replies
  • 1

    [QUOTE=JHus00;1131790]It wont compile! I know something is probably wrong with my if condition but im unsure of what. [CODE]if (weight > 200) shipping_cost = 49.95; else if (weight >= 100 & < 200) shipping_cost = 39.95; else if (weight >= 50 & < 100) shipping_cost = 24.95; else if (weight … Read More

  • 1
    Narue 5,707   7 Years Ago

    [B]>else if (weight >= 100 & < 200)[/B] "else if weight is greater than or equal to 100 and less than 200". That makes perfect sense, but it's wrong. C++ won't infer that in the second half of the expression you're still trying to compare with weight. Further, & is … Read More

1

It wont compile!

I know something is probably wrong with my if condition but im unsure of what.

if (weight > 200)            
                shipping_cost = 49.95;
           else if (weight >= 100 & < 200)
                shipping_cost = 39.95;
           else if (weight >= 50 & < 100)
                shipping_cost = 24.95;
           else if (weight >= 20 & < 50)
                shipping_cost = 15.95;
           else 
                shipping_cost = 11.95;

Thanks

1. Your using bitwise instead of logical ANDs. Change them to &&

2. You can't use standard algebraic notataion. You need to do 2 separate evaluations.
i.e. else if (weight >= 100 && weight < 200)

Edited by Fbody: n/a

1

>else if (weight >= 100 & < 200)
"else if weight is greater than or equal to 100 and less than 200". That makes perfect sense, but it's wrong. C++ won't infer that in the second half of the expression you're still trying to compare with weight. Further, & is the bitwise AND operator, not the logical AND:

else if (weight >= 100 && weight < 200)

Apply that to the other clauses and see how you fare.

Votes + Comments
Thanks
0

Thanks, I figured it out. This works.

if (weight > 200)            
        shipping_cost = 49.95;
     else if (weight >= 100 && weight < 200)
        shipping_cost = 39.95;
     else if (weight >= 50 && weight < 100)
        shipping_cost = 24.95;
    else if (weight >= 20 && weight < 50)
       shipping_cost = 15.95;
     else 
         shipping_cost = 11.95;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.