looping cost calculator

I am learning how to code pretty much entirely on my own, I have a very simple program right now that calculates cost of a package based on weight, however I am wanting to make it so that it does not have a limit on weight and will just keep increasing the cost based on the weight entered.

1
2
3
4
5
6
7
8
9
10
11
  cout << "Enter number of ounces. ";
  cin >> ounces;
  if (ounce <= 20)
     cost = 3.00;
  else if (ounce <= 24)
     cost = 3.50;
  else if (ounce <= 28)
     cost = 4.00
  else
     cout << "Package is too heavy.";
  cout << "Your cost is $ << cost << endl; 


is the part of the code determining and outputting the cost. how would I make it so that instead of having to right true/false statements for every 4 ounces, that insead the program automatically adds 50 cents each time the weight goes up more than 4 ounces after the first 16. For example 16 would be $3.00, anything up to 20 will be $3.50, anything after 20 and up to and including 24 would be $4.00, etc?
the program automatically adds 50 cents each time the weight goes up more than 4 ounces after the first 16.

Can you write a formula for that?

Compute the base cost for the first 16 ounces.
Compute the number of 4 ounce increments over 16 ounces.
Multiply the number of increments by $0.50.

1
2
3
4
5
6
7
8
9
double compute_cost (int oz)
{   double  cost = 3.00; // base cost
    int     incr;
    
    if (oz <= 16)
        return cost;
    incr = (oz - 16) / 4;
    return cost + (incr * 0.50);
}            

Last edited on
That comes close however it keeps the 3.00 charge up til 20 then keeps 3.50 until 23 and then moves to 4.00 at 24, when 17-20 should be 3.50 21-24 should be 4.00 etc. It definately is a math problem just not sure where the issue is.
Topic archived. No new replies allowed.