In: Computer Science
Function Return Value
In this program, you will be using C++ programming constructs, such as functions and loops.
main.cpp
Write a program that allows the user to enter the information for multiple packages to determine the shipping charges for each package. The program will exit when the user enters 0 or negative for the package weight.
Your program will ask the user to enter the weight of a package they want to ship. If the weight they enter is a positive number, your program will then prompt the user to enter the distance the package will be shipped. Your program will then output the shipping charges with a precision of 2 digits past the decimal point, and will prompt the user for the next package.
calculateCharge
Create a function called calculateCharge that contains 2 parameters: a double to represent the weight of the package, and an integer to represent the distance the package will be shipped. This function returns the shipping charge. See types.hpp for the function prototype for this function.
This function calculates the charge based on the package weight as well as the distance. The rates per weight are defined in types.hpp. And that rate is multiplied by how many 500 mile segments the package will be traveling. For instance, if the distance is 1-500 then the rate is multiplied by one. If the distance is 501-1000 then the rate is multiplied by two. 1001-1500, multiplied by three, and so forth.
Input Validation
Hints
Sample Output
Welcome to Fast Freight Shipping Company Enter the package weight in lbs (or 0 to exit): 0
Welcome to Fast Freight Shipping Company Enter the package weight in lbs (or 0 to exit): 33 Enter shipping distance in miles: 3 Shipping cost: $6.40 Enter the package weight in lbs (or 0 to exit): -1
Welcome to Fast Freight Shipping Company Enter the package weight in lbs (or 0 to exit): 3.4 Enter shipping distance in miles: 501 Shipping cost: $8.40 Enter the package weight in lbs (or 0 to exit): 3.4 Enter shipping distance in miles: 500 Shipping cost: $4.20 Enter the package weight in lbs (or 0 to exit): 1.1 Enter shipping distance in miles: 1100 Shipping cost: $9.30 Enter the package weight in lbs (or 0 to exit): 1.1 Enter shipping distance in miles: 1 Shipping cost: $3.10 Enter the package weight in lbs (or 0 to exit): 0
Here is is the information on the types.hpp file to be used:
//-----------
// Constants
//-----------
// shipping distance per segment
const int SEGMENT_MILES = 500;
// rates per 500 miles shipped
const double RATE1 = 3.10; // pkgs weighing <= 2 lb
const double RATE2 = 4.20; // pkgs > 2 lb but <= 6 lb
const double RATE3 = 5.30; // pkgs > 6 lb but <= 10 lb
const double RATE4 = 6.40; // pkgs > 10 lb
//---------------------
// Function prototypes
//---------------------
// This function receives a package weight in lbs and
// a shipping distance in miles. It uses these to compute
// and return the shipping charge.
double calculateCharge(double weight, int distance);
Check out the solution and do COMMENT for any queries.
_________________
// .cpp file
// include required headers
#include <iostream>
#include <iomanip>
#include "types.hpp" // include .hpp file
using namespace std;
// main function
int main() {
// variable declaration
double weight, charge;
int distance;
// infinte loop and loop terminates when user enters 0 or negative
weight
while(true)
{
// display statement as required
cout << "\n\nWelcome to Fast Freight Shipping Company";
cout << "\n\nEnter the package weight in lbs (or 0 to exit) :
";
// user input
cin >> weight;
// if weight > 0 then go ahead
if(weight > 0)
{
// get distance value from user
cout << "Enter shipping distance in miles : ";
cin >> distance;
// function call
charge = calculateCharge(weight, distance);
cout << fixed << showpoint << setprecision(2); //
to print 2 digits after decimal
// print the result
cout << "\nShipping cost : $ " << charge;
}
else
{
// if user enters 0 or negative then print appropriate
message
cout << "\nProgram terminnated!!!";
// break out of the infinite while loop
break;
}
}
return 0;
}
// function definition
double calculateCharge(double weight, int distance)
{
// required declaration
double rate, charge;
int n = 0;
// check out for weight and assign rate accordingly from constants
in .hpp file
if(weight <= 2)
rate = RATE1;
else if(weight > 2 && weight <= 6)
rate = RATE2;
else if(weight > 6 && weight <= 10)
rate = RATE3;
else if(weight > 10)
rate = RATE4;
// as rates are mentioned per 500 miles
// so find number of multiples of 500 present in the given
distance
while(distance > 500)
{
n = distance / 500;
distance = distance % 500;
}
// if distance <= 500 then its as equal to multiplying rate with
1
if(distance <= 500)
n = n + 1; // make n as 1 if distance <= 500 or add 1 to
previously calculated 'n'
// determine the shipping charge
charge = rate * n;
// return the result
return charge;
}
---------------------------
// types.hpp file - given user requirement
// Constants
const int SEGMENT_MILES = 500;
// rates
const double RATE1 = 3.10;
const double RATE2 = 4.20;
const double RATE3 = 5.30;
const double RATE4 = 6.40;
// function prototype
double calculateCharge(double weight, int distance);
----------------------------
Code ::

_____

________
Output ::
