In this assignment, you will develop a C++ program which calculates a shipping c
ID: 3722816 • Letter: I
Question
In this assignment, you will develop a C++ program which calculates a shipping charge and determines the “type of trip” for a freight shipping company.
Ask the user to enter the distance a package is to be shipped, and use a menu and a switch statement to determine the rate based on the package’s weight. Then display the shipping charge and using the table in #7 below, display the type of trip.
Below is the chart to use to calculate the shipping charges.
Freight Shipping Company Rates
______________________________________________________________
Weight of Package (pounds) Rate per 500 miles shipped
_______________________________________________________________
10 pounds or less $1.10
Over 10 pounds but not more than 20 $2.30
Over 20 pounds but not more than 35 $3.60
Over 35 pounds but not more than 50 $4.90
_______________________________________________________________
NOTICE: We do not ship packages over 50 pounds
We do not ship less than 10 miles or more than 15,000 miles
Requirements for your program:
The output must be labeled and easy to read.
Validate that the distance can’t be less than 10 miles and more than 15,000 miles.
Use a logical operator when validating the distance to be shipped.
Display a menu of rate choices (similar to the chart above) so the user can choose the appropriate rate based on the package weight. Have the user enter a choice from the menu and use a switch statement to select and assign the rate.
Calculate the number of segments (a segment is 500 miles). Round uneven segments, i.e., segments not evenly divisible by 500, up to the next whole number (hint: use modulus or ceil).
There is a “long haul charge” of 50 cents per segment if the trip is over 20 segments.
The shipping charge is the number of segments times the shipping rate plus any long haul charge.
Use “if-else” and/or “else-if” and/or “switch” statements to determine the “type of trip” based on the segments and this table:
1 to 10 segments: type of trip is a “short haul”
11 to 20 segments: type of trip is a “medium haul”
21 to 30 segments: type of trip is a “long haul”
Provide two screen prints:
1) the user enters 12,300 miles distance and selects a rate for shipping a 35 pound package.
2) The user enters 1,050 miles distance and selects a rate for shipping a 10 pound package.
Display:
-the distance entered
-the selected rate
-the number of segments
-the package’s shipping charge
-the “type of trip”
Submit:
Your .cpp file
Screen shot (s) of your output
Explanation / Answer
#include <iostream>
#include <iomanip> //used to format output
#include <cstdlib> //used to exit program
using namespace std;
void calculateCharge(double weight, double distance);
int main()
{
//Create and initialize variables.
double weight = 0.0, total = 0.0;
unsigned short int distance = 0; //planes can't fly above +65,535 miles realistically.
cout << "Program calculates shipping costs based on ";
cout << "the weight and distance. Enter 0 for weight to exit. ";
calculateCharge(weight, distance);
}
void calculateCharge(double weight, double distance)
{
//Create variables:
double total = 0.0;
//unsigned short int distance = 0; //planes can't fly above +65,535 miles realistically.
const double twoKg = 3.10; //cost of 2 kg or less.
const double twoSixKg = 4.20; //cost of 2 kg, but less than 6 kg.
const double sixTenKg = 5.30; //cost of 6 kg, but less than 10 kg.
const double overTenKg = 6.40; //cost of anything over 10 kg.
do
{
//Ask user input:
cout << "Enter weight: " << endl;
cin >> weight;
if (weight == 0)
{
break;
}
cout << "Enter distance: " << endl; //distance to travel with item.
cin >> distance;
//Distance less than 500 miles, simply display cost.
if (distance <= 500)
{
cout << fixed << setprecision(2) << endl; //set output. example: 3.10
if (weight <= 2)
{
total = twoKg; //$3.10
cout << "Shipping charge: $" << total << endl;
}
else if (weight > 2 && weight <= 6)
{
total = twoSixKg; //$4.20
cout << "Shipping charge: $" << total << endl;
}
else if (weight > 6 && weight < 10)
{
total = sixTenKg; //$5.30
cout << "Shipping charge: $" << total << endl;
}
else
{
total = overTenKg; //$6.40
cout << "Shipping charge: $" << total << endl;
}
}
system("cls");
/*trying to create loop that will increment 500 miles, multiply shipping charge,
increment another 500 miles to reach the user's input distance.*/
for (int i = 501, p = 2; i <= distance; i = i + 500, p++)
{
cout << fixed << setprecision(2) << endl; //set output. example: 3.10
if (weight <= 2)
{
total = twoKg * p; //$3.10 * rateof500
}
else if (weight > 2 && weight <= 6)
{
total = twoSixKg * p; //$4.20 * 2
}
else if (weight > 6 && weight < 10)
{
total = sixTenKg * p; //$5.30 * 2
}
else
{
total = overTenKg * p; //$6.40 * 2
}
}
cout << "Shipping charge: $" << total << endl;
} while (weight != 0);
}