Hello, I am struggling with getting the correct output from my program, here is the problem followed by my code:
Weight of Package (in Kilograms) | 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
Write a program that asks for the weight of the package and the distance it is to be shipped, and then displays the charges.
Input validation: Do not accept values of 0 or less for the weight of the package.Do not accept weights of more than 20 Kg (this is the maximum weight the company will ship). Do not accept distances of less than 10 miles or more than 3,000 miles.
These are the company’s minimum and maximum shipping distances.
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
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double packageWeight, packageDistance, miles, rate, shippingTotal,distanceUnit;//Declaring my variables
cout<<setprecision(2)<<fixed;
cout<<"Enter the weight of your package (kilograms)"<<endl;//Prompt user to input weight
cin>>packageWeight;//Store weight in packageWeight
if (packageWeight<=0||packageWeight>20)//Values of 0 or less and more than 20 are not accepted
{
cout<<"I'm sorry, the weight you entered cannot be accepted"<<endl;
}
cout<<"Enter the distance your package is being shipped (miles)"<<endl;//Prompt user to input distance
cin>>packageDistance;//Store it in packageDistance
if(packageDistance<10||packageDistance>3000)
{
cout<<"I'm sorry, the distance you entered cannot be accepted"<<endl;//Distances less than 10 and more than 3000 are not accepted
return 0;
}
if (packageWeight<=2)
{
rate=1.10;
}
else if(packageWeight>2&&packageWeight<=6)
{
rate=2.20;
}
else if (packageWeight>6&&packageWeight<=10)
{
rate=3.70;
}
else if (packageWeight>10&&packageWeight<=20)
{
rate=4.80;
}
if (miles > 10 && miles < 500)
{
miles = 1;
}
else if (miles > 500 && miles < 1000)
{
miles = 2;
}
else if (miles > 1000 && miles < 1500)
{
miles = 3;
}
else if (miles > 1500 && miles < 2000)
{
miles = 4;
}
else if (miles > 2500.0 && miles <= 3000)
{
miles = 5;
}
shippingTotal=packageDistance*rate*miles;
cout<<"The total for your shipping charges are: $"<<shippingTotal<<endl;
return 0;
}
|