PDA

View Full Version : c++ question




chris200x9
Oct 31, 2007, 08:59 PM
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



hankolerd
Oct 31, 2007, 11:50 PM
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


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!!!

stupidregister
Nov 1, 2007, 12:37 AM
hankolerd, you could use the code tags to preserve formatting.

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

Aranince
Nov 1, 2007, 01:03 AM
hankolerd, you could use the code tags to preserve formatting.

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

That should work...just don't forget the semicolons!

madmoose
Nov 1, 2007, 10:02 AM
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);
}

toddburch
Nov 1, 2007, 10:43 AM
Based on this requirement:


plus $0.75 for each ounce, or part thereof,


Shouldn't this be considered:


float shippingCharge(float pounds, float ounces)
{
float weight = pounds * 16 + ounces;
if ((int) weight < weight) { weight = (int) (weight+1) } ;
...
}

(not syntax checked)

Todd

madmoose
Nov 2, 2007, 04:22 AM
Good catch. I'd prefer float weight = ceil(pounds * 16.0 + ounces); though.

toddburch
Nov 2, 2007, 07:56 AM
Good catch. I'd prefer float weight = ceil(pounds * 16.0 + ounces); though.

Yep. that's better.