In: Computer Science
In c++ format please
ABC Delivery promises to deliver any package from one location in Miami Dade County to another in 2 to 24 hours. They can handle packages from 0.1 to 100 pounds. Their rate table is below:
Write a program that asks the user for the package weight. If the range is acceptable, ask for the delivery speed. If the range is acceptable, proceed with calculating the delivery cost.
Prompt:
Enter a shipping weight (in pounds) up to 100 lbs:
How soon do you need this delivered? Enter a number of hours between 2 and 24:
Possible Outputs (the numbers do not output, they are for clarification only)
1) Invalid weight
2) Invalid number of hours
3) We can make that delivery!
The total price of your 100-pound package delivered within 5 hours
is $150
Notes and Hints:
1) Nothing weighs zero pounds. Reject that type of entry.
2) Hours must be a whole number entry from user. Weight can include decimal.
3) There are many ways to solve this. You only need what you learned in class today (nothing from future lessons). The best solutions are those that minimize CPU time by reducing how many conditions it has to check. Be creative and think this through before you start coding!
As per your requirement, here is the C++ code:
Rawcode:
//including libraries
#include <iostream>
#include <stdlib.h>
//using namespace
using namespace std;
//main function
int main(){
//declaring variables
double weight, totalPrice;
int hours, flatRate = 50, surcharge;
//prompting user to enter shipping weight
cout << "Enter a shipping weight (in pounds) up
to 100 lbs: ";
//scanning it into a variable
cin >> weight;
//checking if weight is less than or equal to 0 or
weight is greater than 100
if(weight <= 0 || weight > 100){
//if above condition is true then
print user that it is invalid weight
cout << "Invalid weight"
<< endl;
//terminating the program
exit(EXIT_FAILURE);
}
//asking user to enter the number of hours he needed
to deliver the package
cout << "How soon do you need this delivered?
Enter a number of hours between 2 and 24: ";
//scanning it into a variable
cin >> hours;
//checking if the hours entered by user is less than
or equal to 24 and greater than or equal to 2
if(hours <= 24 && hours >= 2){
//checking if hours are between 2
and 4
if(hours >= 2 && hours
<= 4){
//if above
condition is true then set surcharge of 100
surcharge =
100;
//calculating
total price
totalPrice =
(0.50 * weight) + flatRate + surcharge;
//printing that
we can make delivery
cout <<
"We can make that delivery!" << endl;
//printing the
total price
cout <<
"The total price of your "<< weight << "-pound package
delivered within " << hours << " hours is $" <<
totalPrice << endl;
}
//checking if hours are between 5
and 7
else if(hours >= 5 &&
hours <= 7){
//if above
condition is true then set surcharge of 50
surcharge =
50;
//calculating
total price
totalPrice =
(0.50 * weight) + flatRate + surcharge;
//printing that
we can make delivery
cout <<
"We can make that delivery!" << endl;
//printing the
total price
cout <<
"The total price of your "<< weight << "-pound package
delivered within " << hours << " hours is $" <<
totalPrice << endl;
}
//checking if other hours entered
between 2 and 24
else{
//calculating
total price
totalPrice =
(0.50 * weight) + flatRate;
//printing that
we can make delivery
cout <<
"We can make that delivery!" << endl;
//printing the
total price
cout <<
"The total price of your "<< weight << "-pound package
delivered within " << hours << " hours is $" <<
totalPrice << endl;
}
}
//if above condition is not true
else{
//then printing that they are
invalid hours
cout << "Invalid number of
hours" << endl;
}
return 0;
}
Here is the screenshots of source code:
Here is the screenshot of output:
Hope this helps you. Please feel free to ask if you still have any queries.
Do upvote. Thank you !