c++ question

Discussion in 'Mac Programming' started by chris200x9, Oct 31, 2007.

  1. macrumors 6502a

    Joined:
    Jun 3, 2006
    #1
    how would I go about creating a program to calculate shipping charges?


    i.e. If the order weight is less than 8 ounces, there is a flat rate charge of $2.00.
    If the order weight is between 8 ounces and 32 ounces, the charge is $2.00
    plus $0.75 for each ounce, or part thereof, over 8 ounces.
    If the order weight is greater than 32 ounces, the charge is $20.00 plus
    $1.15 for each ounce over 32 ounces.


    I would now how to the only problem is it excepts pounds and ounces

    so someone can put in 0.5 pounds and it should be 2.00 also
     
  2. macrumors 6502

    hankolerd

    Joined:
    Sep 19, 2007
    Location:
    Seattle, WA
    #2

    1) make sure whatever the value is it is in ounces
    2)
    ---if (weight > 8)
    ---{
    ---------if (weight > 32)
    ---------{
    --------------price = 20.00
    --------------weight -= 32
    --------------while (weigh != 0)
    --------------{
    ------------------price += 1.15
    ------------------weight -= 1
    ---------------}
    ---------}
    ---------else
    ---------{
    -------------price = 2.00
    -------------weight -= 8
    -------------while (weight != 0)
    -------------{
    ------------------price += .75
    ------------------weight -= 1
    -------------}
    --------}
    --}
    --else
    ------price = 2.00


    hope this design helps:apple:


    EDIT: WHY DOESNT THE SPACES EVER SHOW UP!!!
     
  3. macrumors member

    Joined:
    Sep 29, 2007
    #3
    hankolerd, you could use the code tags to preserve formatting.

    Code:
    if (weight > 8)
    {
        if (weight > 32)
        {  
            price = 20.00
            weight -= 32
            while (weigh != 0)
            {
                price += 1.15
                weight -= 1
            }    
        }   
        else
        {   
            price = 2.00
            weight -= 8
            while (weight != 0) 
            {
                price += .75
                weight -= 1
            }
        }
    }
    else
        price = 2.00
     
  4. macrumors 65816

    Joined:
    Apr 18, 2007
    Location:
    California
    #4
    That should work...just don't forget the semicolons!
     
  5. macrumors newbie

    Joined:
    Oct 5, 2007
    #5
    Code:
    float shippingCharge(float pounds, float ounces)
    {
    	float weight = pounds * 16 + ounces;
    	
    	// weight from 0 to 8 ounces
    	if (weight <= 8.0)
    		return 2.0;
    	
    	// weight from 8 to 32 ounces
    	if (weight <= 32.0)
    		return 2.0 + 0.75 * (weight - 8.0);
    	
    	// weight over 32 ounces
    	return 20.0 + 1.15 * (weight - 32.0);
    }
     
  6. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #6
    Based on this requirement:

    Shouldn't this be considered:

    (not syntax checked)

    Todd
     
  7. macrumors newbie

    Joined:
    Oct 5, 2007
    #7
    Good catch. I'd prefer
    Code:
    float weight = ceil(pounds * 16.0 + ounces);
    though.
     
  8. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    Yep. that's better.
     

Share This Page