This is the assignment I am having trouble with, sorry for the wall of text but I feel it is necessary to get help.
The Fast Freight Shipping Company charges the following rates:
Weight of Package (in Kiolograms) Rate per 500 Miles Shipped
2 Kg or less $1.10
Over 2 Kg but not more than 6 Kg $2.20
Over 6 Kg but not more than 10 Kg $3.70
Over 10 Kg but not more than 20 Kg $4.80
make sure the user entered a correct weight
make sure the user entered a correct distance
add an if/else if statement that uses logical operators to write a range for each row in the rate table
add an if/else if statement that uses simple conditions and orders the conditions to determine the rate for each row in the rate table
I am having trouble figuring out how to add the if statements and getting the right expressions
this is the code I have so far
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include<iostream>
#include<iomanip>
using namespace std;
/*This program gets the weight of a package and
the distance it is to be shipped. Then it computes
the shipping charges for that package.
*/
int main(){
const int DISTANCE_UNIT = 500;
double rate=0, shippingCost;
int weight, distance, distanceUnits;
//set up output formatting
cout<<setprecision(2)<<fixed;
//get weight
cout<<"How much does the package weigh (in Kilograms)? ";
cin>>weight;
if(weight<=0||weight>20) //The Fast Freight Shipping Company does not accept values of 0 or less for the weight of a package.
{cout<<"Sorry, the weight you entered, "<<weight<<"Kg, is not in the range of weight Fast Freight Shipping is able to ship"<<endl;
return 0;}
else
//get distance
cout<<"How far will the package be going? ";
cin>>distance;
if(distance<10||distance>3000) // The Fast Freight Shipping Company does not accept distances of less than 10 miles or more than 3000 miles.
{cout<<"Sorry, the distance you entered, "<<distance<<" miles, is not in the range of weight Fast Freight Shipping is able to ship"<<endl;
return 0;}
else
//compute how many distance units
distanceUnits = distance/DISTANCE_UNIT;
if(distance%DISTANCE_UNIT != 0)
//deteremine rate using complex conditions with
//logical operators to identify ranges
if(weight<=2&&distance<=500)
{rate=1.10;}
else if (weight<=2&&distance>500&&distance<=3000)
{rate=2.20;}
else if (weight<=2&&distance>50
//calculate shipping cost
shippingCost = distanceUnits * rate;
//print shipping cost
cout<<"\nUsing logical operators to identify ranges:\n"
<<"The cost to ship a package that weighs "<<weight
<<" Kilograms\nfor a distance of "<<distance
<<" miles is $"<<shippingCost<<endl<<endl;
//deteremine rate using if/else if with simple conditions
//using order to correctly separate the options
//calculate shipping cost
shippingCost = distanceUnits * rate;
//print shipping cost
cout<<"Using the order of the if/else if statement:\n"
<<"The cost to ship a package that weighs "<<weight
<<" Kilograms\nfor a distance of "<<distance
<<" miles is $"<<shippingCost<<endl<<endl;
return 0;
}
|