How do businesses like Amazon™ calculate shipping? The cost of shipping can include the size and weight of an item, where it’s coming from, where it’s going to, and whether multiple items can be packaged together. Unless you have a premium membership and/or order items over a certain threshold, you have to pay for shipping. This application calculates shipping charges for single packages using the weight and distance in miles.
Detailed Information
This application calculates shipping charges based upon the following rates:
Weight of package in Kilograms Rate per 500 Miles Shipped
2 Kg or less $1.10
( 2 Kg, 6 Kg] $2.20
(6 Kg, 10 Kg] $3.70
(10 Kg, 20 Kg] $4.80
The application asks the user for the weight of a single package in kilograms. If the user enters 0, the program ends here. The loop sentinel value is 0 for weight. After getting a non-zero weight, the program asks the user for the distance the package is to be shipped. If the user enters a weight less than 0 or a weight greater than 20, print one of the error messages below and continue asking until the user either enters 0 to end the program or the user enters a valid weight. Similarly, if the user enters an invalid distance, less than 10 or greater than 3,000, print an error message and continue asking until the user enters a valid distance (the user may not choose to end the program at this point). Use the following error messages.
Weight < 0 “Weight must be greater than zero.”
Weight > 20 Kg “We do not ship items weighing more than 20 kilograms.”
Distance < 10 or > 3,000 “Distance must be between 10 and 3,000 miles”
After the user has entered valid inputs for weight and distance of a single package, the program prints the cost of shipping for that package. When the program ends it prints the total shipping costs and the average cost per package.
Make sure the program formats numeric monetary outputs with exactly two digits to the right of the decimal and that the remaining output is worded as shown below in the Sample Execution.
Declare globally and use at least 7 constants.
its not ending after you enter zero
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <iostream>
#include <cmath>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
double weight, rate, totalCost;
int X, distance;
cout << "\n Welcome to Shippers R Us!" << endl;
cout << "\nPackage Weight(in KG, 0 to end): ";
cin >> weight;
if (weight > 20)
{cout << "\n We do not ship items weighing more than 20 kilograms." << endl;
system("pause");
return (0);}
cout << "\nDistance (in miles): ";
cin >> distance;
if (distance < 10 || distance > 3000)
{cout << "Distance must be between 10 and 3,000 miles" << endl;
return (0);}
if (weight <= 2){
rate = 1.10;}
else if (weight > 2 && weight < 6){
rate = 2.20;}
else if (weight > 6 && weight < 10){
rate = 3.70;}
else if (weight > 10 && weight < 20){
rate = 4.80;}
// not calculating (rounding) the distance
X = distance / 500.0;
totalCost = X * rate;
cout << setprecision(2) << "\nPackage weight ( in KG, 0 to end): " << weight;
cout << setprecision(2) << "\nDistance (in miles): " << distance;
cout << setprecision(2) << "\nShipping cost $" << totalCost <<"\n";
return(0);
}
| |