A quick question...

This is a discussion on A quick question... within the C Programming forums, part of the General Programming Boards category; I am new to C programming and am unsure how to fix this issue. the problem is with my if ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    8

    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;
           }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    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.

  4. #4
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    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..
    You ended that sentence with a preposition...Bastard!

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    8
    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

  6. #6
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    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
    You ended that sentence with a preposition...Bastard!

  7. #7
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Maybe "else" is that word.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  8. #8
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    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.

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    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.
    You ended that sentence with a preposition...Bastard!

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21