Answer to Question #161800 in C++ for Anonymous

Question #161800

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:

 

  • Flat rate for all deliveries = $50
  • To that they add $0.50 per pound
  • To that they add a speed surcharge of $100 if request is between 2 and 4 hours and $50 if request is between 5 to 7 hours.

 

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!


1
Expert's answer
2021-02-10T06:07:18-0500
#include <iostream>
using namespace std;

#define FLAT_RATE 50
#define COST_PER_POUND 0.5

int main() {
  double weight;
  int hours;
  double cost;
  int speedSurcharge = 0;

  cout << "Enter a shipping weight (in pounds) up to 100 lbs: ";
  cin >> weight;

  if ( weight <= 0 || weight > 100 ) {
    cout << "Invalid weight";
    return 0;
  }

  cout << "How soon do you need this delivered? Enter a number of hours between 2 and 24: ";
  cin >> hours;

  if ( hours < 2 || hours > 24 ) {
    cout << "Invalid number of hours";
    return 0;
  }

  if ( hours < 5 ) {
    speedSurcharge = 100;
  } else if ( hours < 8 ) {
    speedSurcharge = 50;
  }

  cost = FLAT_RATE + ( weight * COST_PER_POUND ) + speedSurcharge;

  cout << "\nWe can make that delivery!\n";
  cout << "The total price of your " << weight << "-pound package delivered within " << hours << " hours is $" << cost << "\n";

  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog