Write a C++ Program:
Some Shipping Company charges the following rates:
|
Weight of Package (in Kilograms) |
Rate per 300 Miles Shipped |
Insurance rate per 1 kg |
|
2 Kg or less |
$1.10 |
$1.5 |
|
Over 2 Kg but not more than 6 Kg |
$2.20 |
$1.5 |
|
Over 6 Kg but not more than 10 Kg |
$3.70 |
$2.0 |
|
Over 10 Kg but not more than 16 Kg |
$4.00 |
$2.5 |
|
Over 16 Kg but not more than 20 Kg |
$4.80 |
$3.0 |
Design a C++ program that asks for the weight of the package and the distance it is to be shipped.
The insurance is optional; you must ask if the customer want it or not.
Your program should display the charges with and without insurance.
Input Validation: Do not accept values of 0 or less for the weight of the package. Do not accept weight of more than 20 Kg (this is the maximum weight the company will ship). Do not accept distances of less than 10 miles or more than 3,000 miles. These are the company’s minimum and maximum shipping distances.
Source Code in C++:
#include <iostream>
using namespace std;
int main()
{
int weight,distance; //variables to store weight and distance
cout << "Weight of package (in kilograms): "; //input
prompt
cin >> weight; //input
//validating weight input
if(weight<=0)
{
cout << "Weight cannot be negative or zero." <<
endl;
return 1;
}
else if(weight>20)
{
cout << "Weight cannot be more than 20 kilograms." <<
endl;
return 1;
}
cout << "Distance (in miles): "; //input prompt
cin >> distance; //input
//validating weight input
if(distance<10)
{
cout << "Distance cannot be less than 10 miles." <<
endl;
return 1;
}
else if(distance>3000)
{
cout << "Distance cannot be more than 3000 miles." <<
endl;
return 1;
}
double charge,insuranceRate; //variables to store rate of charge
and rate of insurance
//finding rate of charge and rate of insurance according to
weight
if(weight<=2)
{
charge=1.10;
insuranceRate=1.5;
}
else if(weight>2 && weight<=6)
{
charge=2.20;
insuranceRate=1.5;
}
else if(weight>6 && weight<=10)
{
charge=3.70;
insuranceRate=2.0;
}
else if(weight>10 && weight<=16)
{
charge=4.00;
insuranceRate=2.5;
}
else
{
charge=4.80;
insuranceRate=3.0;
}
double total;
//finding shipping charge total in terms of distance
if(distance%300==0)
total=charge*distance/300;
else
total=charge*((distance/300)+1);
//displaying both options
cout << "Your total without insurance: $" << total
<< endl;
cout << "Your total with insurance: $" <<
total+insuranceRate*weight << endl;
int insuranceChoice;
cout << "Do you want insurance? Enter 1 for yes and anything
else for no: ";
cin >> insuranceChoice; //taking user choice
//output
if(insuranceChoice==1)
cout << "Amount to be paid: $" <<
total+insuranceRate*weight << endl;
else
cout << "Amount to be paid: $" << total <<
endl;
return 0;
}
Output:

Write a C++ Program: Some Shipping Company charges the following rates: Weight of Package (in Kilograms)...