# c++ question

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

1. ### chris200x9 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 ﬂat 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. ### hankolerd macrumors 6502

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

EDIT: WHY DOESNT THE SPACES EVER SHOW UP!!!

3. ### stupidregister 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. ### Aranince macrumors 65816

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

5. ### madmoose 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. ### toddburch macrumors 6502a

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

Shouldn't this be considered:

(not syntax checked)

Todd

7. ### madmoose macrumors newbie

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

8. ### toddburch macrumors 6502a

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