# A quick question...

• 02-10-2011
MarioI
A quick question...
I am new to C programming and am unsure how to fix this issue. the problem is with my if statement. I need to use numbers between 10-30 and 30-50. I had everything working and I don't know what I did messing around but I can't figure out what I did. I went to a T/A at my University but they are gay about helping and wont actually show me what I am doing wrong. any help would greatly be appreciated. here is the code

Code:

```if (item_weight <30) {                  shipping_total = item_weight*2.50;       }           if (item_weight < 50) {       shipping_total =item_weight*1.50;       }```
• 02-10-2011
CommonTater
Well, your TA is not "gay" about anything. He or she is right... you do have to learn how to figure these things out on your own. 75% of programming skill is the ability to figure out why it doesn't work.

Look at the sequence of events you've invented here... exactly what happens when you tell it item_weight = 20... Follow the value through the conditions in your code...

Now try to think how you could prevent the problem...

Hint: It's a one word fix.
• 02-10-2011
MarioI
Quote:

Originally Posted by CommonTater
Well, your TA is not "gay" about anything. He or she is right... you do have to learn how to figure these things out on your own. 75% of programming skill is the ability to figure out why it doesn't work.

I agree it just can be frustrating at times. especially since I stayed up so late trying to figure it out. you say its a 1 word fix... I was thinking that maybe I didn't set a range. if its suppose to be any numbers from 10-30 and then 30-50. I was reading online and thought that using rand() but it didn't work out.
• 02-10-2011
Eman
if it is < 30 wouldn't that include everything below 30? :O
and the same for <50..and you don't want that I assume..
• 02-10-2011
MarioI
well that's what I noticed is that if put <30 then it will cancel out the first part which is <10. I'm trying to figure out how to fix those to pieces of code so that they don't cancel out. Commontater said it was a 1 word fix... so I'm trying to think about it now
• 02-10-2011
Eman
to fix it is simple..but I am not sure about the one word fix either..especially as there is a bug in the code.

in words:
if item_weight is greater than or equals to 10 and item_weight is less than or equals to 30
do this..
else if..
^^^
but I don't see a one word fix...mmn
• 02-10-2011
nimitzhunter
Maybe "else" is that word.
• 02-10-2011
Babkockdood
Try using an else-if for if (item_weight < 50). This will make it so if (item_weight < 50) only gets evaluated if (item_weight < 30) is false.
• 02-10-2011
Eman
i think it should be..
if(item_weight >= 10 && item_weight<=30)
...
...
else if(item_weight>=30 && item_ weight <= 50)
...
else
...//to account for other numbers not in this range.
• 02-10-2011
CommonTater
Code:

```if (item_weight <30) {                  shipping_total = item_weight*2.50;       }     else        if (item_weight < 50) {         shipping_total =item_weight*1.50;       }```
Follow some numbers through the code...
anything less than 30 is caught by the first if clause, and the else stops the second one from running....
Anything greater than 30 falls through the first one and is caugh by the second one.
Anything greater than 50 fall through the whole mess and does nothing.