### shipping charges

I am having trouble getting the final total amount correct, what am I doing wrong? please help!

A shipping company calculates charges based on parcel's weight and distance to be shipped.
The weight charges are:
0 to 10 pounds - > \$0.75 per pound
10 to 15 pounds - > \$0.85 per pound
15 to 20 pounds - > \$0.95 per pound
over 20 pounds NOT ALLOWED
Distance charges are:
0 to 50 miles - > \$0.07 per mile
50 to 100 miles - > \$0.06 per mile
100 to 200 miles - > \$0.05 per mile
200 to 500 miles - > \$0.04 per mile
over 500 miles NOT ALLOWED
Create a program proj3_1.cpp to compute the total charge based on the two schedules above. Your program should ask the user for the parcel's wight and distance to be shipped. Then it should compute and add the two charges into the final one to be displayed. IMPORTANT: Your program should display an ERROR for values that are not allowed for either weight, miles, or both. Not allowed values are negative or over the limit from the two schedules. Your program will be tested for such values.
Sample run:

Welcome to Cristian's Shipping Company!
Please enter the distance in miles to be shipped: 56
The total is computed as follows: 2.5 * 0.75 + 56 * 0.06, where of course the weight and miles are variables and not numbers.
Hint: Your program should have two series of if/else if statements: one to determine charge by weight and the other for charge by miles. In the end add and display the two charges or an error message for invalid input.

#include <iostream>
using namespace std;

int main (void)
{
double weight,
shipcharge,
distance;

float total_charge;

cout << "Welcome to Christian's Shipping Company ";
cout << "\n";
cout << "Please enter the weight in pounds of your package: ";
cin>>weight;

if (weight >= 0)
shipcharge = (0.75);
else if (weight > 0 && weight <= 10)
shipcharge = (weight * 0.75);
else if (weight > 10 && weight <= 15)
shipcharge = (weight * 0.85);
else if (weight > 15 && weight <= 20)
shipcharge = (weight * 0.95);

if (weight < 0 || weight > 20)
cout << "Error incorrect input";

cout << "Please enter the distance in miles to be shipped: ";
cin>>distance;
if (distance >= 0)
shipcharge = (0.07);
else if (distance > 0 && distance <= 50)
shipcharge = (distance * 0.07);
else if (distance > 50 && distance <= 100)
shipcharge = (distance * 0.06);
else if (distance > 100 && distance <= 200)
shipcharge = (distance * 0.05);
else if (distance <= 500)
shipcharge = (distance * 0.04);

if (distance < 0 || distance > 500)
cout << "Error incorrect input";

// coumpute total charge
total_charge = (weight * shipcharge) + (distance * shipcharge);

// display results
cout << "Your total charge is :\$ " << total_charge << endl;

system("Pause");
return 0;
}

Last edited on
Here's what I noticed with your codes. I didn't run your code.

 12345678910111213141516171819202122232425262728293031323334 //I think a weight_charge total would be better if (weight >= 0) //Don't need this line shipcharge = (0.75); //don't need this line & also formula incomplete else if (weight > 0 && weight <= 10) //this line covers the weight>0 but change the else if shipcharge = (weight * 0.75); else if (weight > 10 && weight <= 15) shipcharge = (weight * 0.85); else if (weight > 15 && weight <= 20) shipcharge = (weight * 0.95); if (weight < 0 || weight > 20) cout << "Error incorrect input"; //I think a distance_charge total would be better cout << "Please enter the distance in miles to be shipped: "; cin>>distance; if (distance >= 0) //Don't need this line shipcharge = (0.07); //Don't need this line & also formula incomplete else if (distance > 0 && distance <= 50) //this line covers weight>0 but change the else if shipcharge = (distance * 0.07); else if (distance > 50 && distance <= 100) shipcharge = (distance * 0.06); else if (distance > 100 && distance <= 200) shipcharge = (distance * 0.05); else if (distance <= 500) //missing weight>200 in the formula shipcharge = (distance * 0.04); if (distance < 0 || distance > 500) cout << "Error incorrect input"; // coumpute total charge total_charge = (weight * shipcharge) + (distance * shipcharge); //total_charge = weight_charge + distance_charge;

Last edited on
You should not compare floating point numbers directly. They are stored as binary fractions & cannot represent every real number. Google numeric_limits<double>epsilon

This code fails:

 123 float a = 0.1; // a is 0.09999997 if (a * 10.0 == 1.0) //fail 10.0 * a is 0.99999997

Changing the type to double doesn't help - the extended precision doesn't mean there will be less occurrences of the problem.

To do comparisons, first work out the absolute value of the difference between your number and the comparison number. Compare this to the scaled epsilon value.

Remember to scale the epsilon appropriately. If your number is 500 then you need to use 500.0 * numeric_limits<double>epsilon as your epsilon value.

HTH Good LucK !!!!!!!!!!!!
Last edited on
I believe the biggest problem here is that you need a rate for the distance and a rate for the weight of the package. You're attempting to store both rates in one variable (shipcharge) and of course you can only store one value in the variable, so the second calculated overwrites the first. I would suggest using two separate variables for the distance and weight rate.
thank you so much. Figured it out and you guys were right.
Topic archived. No new replies allowed.