62.5k views
4 votes
Write a program that asks for the weight of a package and the distance it is to be shipped. This information should be passed to a calculateCharge function that computes and returns the shipping charge to be displayed . The main function should loop to handle multiple packages until a weight of 0 is entered.

User Dankirkd
by
4.5k points

1 Answer

2 votes

Answer:

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

const int WEIGHT_MIN = 0,

WEIGHT_MAX = 20,

DISTANCE_MIN = 10,

DISTANCE_MAX = 3000;

float package_weight,

distance,

total_charges;

cout << "\\What is the weight (kg) of the package? ";

cin >> package_weight;

if (package_weight <= WEIGHT_MIN ||

package_weight > WEIGHT_MAX)

{

cout << "\\We're sorry, package weight must be\\"

<< " more than 0kg and less than 20kg.\\"

<< "Rerun the program and try again.\\"

<< endl;

}

else

{

cout << "\\Distance? ";

cin >> distance;

if (distance < DISTANCE_MIN ||

distance > DISTANCE_MAX)

{

cout << "\\We're sorry, the distance must be\\" << "within 10 and 3000 miles.\\"

<< "Rerun the program and try again.\\"

<< endl;

}

else

{

if (package_weight <= 2)

total_charges = (distance / 500) * 1.10;

else if (package_weight > 2 &&

package_weight <= 6)

total_charges = (distance / 500) * 2.20;

else if (package_weight > 6 &&

package_weight <= 10)

total_charges = (distance / 500) * 3.70;

else if (package_weight > 10 &&

package_weight <= 20)

total_charges = (distance / 500) * 4.80;

cout << setprecision(2) << fixed

<< "Total charges are $"

<< total_charges

<< "\\For a distance of "

<< distance

<< " miles\\and a total weight of "

<< package_weight

<< "kg.\\"

<< endl;

}

}

Step-by-step explanation:

User Samuel Negru
by
5.0k points