I have an assignment here for a value returning function that uses another function to obtain the values, if any one could help me this is the description: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 10 lbs $0.30
Over 10 lbs but not more than 20 lbs $0.45
Over 20 lbs $0.50
Write a function in a program that asks for the weight of a package in pouds and the distance it is to be shipped. Using that data, write another function 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 function, if the shipping distance the user provided earlier is a distance greater than 20 miles, there will be an additional charge of $25 added to the order. For example, if an item weighing 21 pound is shipped 25 miles, the total cost will be $37.50, calculated by the formula: ((25 miles * 0.50) + 25).
input validation: Do not accept any weights less than 1 pound, nor any distances less than 0 miles.
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
|
#include <iostream>
using namespace std;
float rate;
void input();
float calculation (float weight, float distance);
int main()
{
float total;
total = calculation();
cout << "Your total cost is " << total;
}
void input ()
{
cout << "How much does your shipment weigh in pounds? ";
cin >> weight;
cout << "What is the distance of the shipment in miles? ";
cin >> distance;
while (weight < 1 || distance < 0)
{
cout << "invalid input please try again\n";
cout << "How much does your shipment weigh in pounds? ";
cin >> weight;
cout << "What is the distance of the shipment in miles? ";
cin >> distance;
}
float calculation (weight, distance)
{
float totalCost;
if (weight <= 5)
rate = 0.20;
else if (weight > 5 && weight <= 10)
rate = 0.30;
else if (weight > 10 && weight <= 20)
rate = 0.45;
else if (weight > 20)
rate = 0.50;
totalCost = weight * rate;
if (distance > 20)
totalCost += 25;
return totalCost;
}
}
|