You want to write a program that will generate accurate shipping charges for a local grocery store. The shipping charges for the store are as follows: Weight of Package (in pounds) Rate per mile Shipped 5 Lbs or less $0.20 Over 5 Lbs but no more than 8 Lbs $0.35 Over 8 Lbs but not more than 16 Lbs $0.45 Over 16 Lbs $0.50 Do NOT use global variables for this exercise. Write a function in a program that asks for the weight of a package in pounds and the distance it is to be shipped. These two arguments are to be passed into the funciton by REFERENCE. Using that data, write a second function (called by the main program) that calculates the shipping charge and returns it to the main program and displays the value inside the main program. For the second function, pass the two values (weight and distance) into the function as arguments. In the second function, if the shipping distance the user provided earlier is a distance greater than 20 miles, there will be an additional "Extended Distance" charge of $50 added to the order. For example, if an item weighing 21 pounds is shipped 25 miles, the total cost will be $62.50, calculated by the formula: ((25 miles * 0.50) + 50). Output to the user might read: A package weighing 21 pounds being shipped 25 miles will cost $62.50, which includes an "extended" distance charge of $50. input validation: Do not accept any weights less than 1 pound, nor any distances less than 0 miles.
-
You want to write a program that will generate accurate shipping charges for a local grocery store. The shipping charges for the store are as follows:
Weight of Package (in pounds) Rate per mile Shipped
5 Lbs or less $0.20
Over 5 Lbs but no more than 8 Lbs $0.35
Over 8 Lbs but not more than 16 Lbs $0.45
Over 16 Lbs $0.50Do NOT use global variables for this exercise.
Write a function in a program that asks for the weight of a package in pounds and the distance it is to be shipped. These two arguments are to be passed into the funciton by REFERENCE.
Using that data, write a second function (called by the main program) that calculates the shipping charge and returns it to the main program and displays the value inside the main program. For the second function, pass the two values (weight and distance) into the function as arguments. In the second function, if the shipping distance the user provided earlier is a distance greater than 20 miles, there will be an additional "Extended Distance" charge of $50 added to the order. For example, if an item weighing 21 pounds is shipped 25 miles, the total cost will be $62.50, calculated by the formula: ((25 miles * 0.50) + 50).
Output to the user might read:
A package weighing 21 pounds being shipped 25 miles will cost $62.50, which includes an "extended" distance charge of $50.
input validation: Do not accept any weights less than 1 pound, nor any distances less than 0 miles.
code:
#include <iostream>
using namespace std;
double getInfo();
double calculateCharges(double,double);
int main()
{
//calling getInfo() function to get info and printing the rerturn value
cout<<"The Shipping charges of Package are $"<<getInfo()<<endl;
return 0;
}
//function to get weight and distance from user
double getInfo()
{
double weight,distance;
do{
//getting input
cout<<"Enter weight of the package(in pounds) : ";
cin>>weight;
cout<<"Enter the distance of package to be shipped(in miles) :";
cin>>distance;
//if input is not valid ask the values again using loop
if(weight<1 && distance<0)
cout<<"Weight can't be less than 1 pounds and Distance can't be less than 0 miles"<<endl;
//otherwise break out of loop
else
break;
}while(true);
//calling calculateCharges() function with passing weight and distanc and returning the return value to main
return calculateCharges(weight,distance);
}
//function to calculate Charges of package
double calculateCharges(double weight,double distance)
{
double charges=0,chargesPerMile=0;
//calculating charges per mile according to weight of package
if(weight<=5)
chargesPerMile=chargesPerMile+0.20;
else if(weight<=10)
chargesPerMile=chargesPerMile+0.30;
else if(weight<=20)
chargesPerMile=chargesPerMile+0.45;
else if(weight>20)
chargesPerMile=chargesPerMile+0.50;
//calculating shipping charges according to distance of package to be shipped
if(distance>20)
charges=chargesPerMile*distance+50;
else
charges=chargesPerMile*distance;
//returning charges value
return cout<<" The shipping charges of the Package are= $", charges;
}
Step by step
Solved in 3 steps with 2 images