How to calculate in increments

Hello, I am just starting out and need some advice on a code I am working on.
The code works but I cant figure out how to do one thing. That is to get it to calculate in increments of 500 miles. Any help is appreciated thanks.



#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float rate = 0, weight = 0, miles = 0, shipping = 0, cost = 0;

cout << "Enter your package weight: " << endl;
cin >> weight;

// Shipping limits
if (weight <= 0)
{
cout << "weight should be greater than 0" << endl;
}
else if (weight > 20)
{
cout << "max weight should 20" << endl;
}

//user enters the distance
cout << "Enter shipping distance: " << endl;
cin >> miles;



if (miles < 20 || miles > 3000)
{
cout << "These are the companies min and max " << endl;
}

//shipping cost calculation
shipping = (miles / 500);
if (weight <= 2)
{
cost = (shipping * 1.10);
}
else if (weight > 2 || weight <= 6)
{
cost = (shipping * 2.20);
}
else if (weight > 6 && weight <= 10)
{
cost = (shipping * 3.70);
}
else if (weight > 10 && weight <= 20)
{
cost = (shipping * 4.80);
}

cout << setprecision(2) << fixed;
cout << "Shipping cost is: $" << cost << endl;






return 0;
}

Last edited on
Hey,

yes, you're correct. This code works. But it works not like you want it to work :) There's a bug.
Instead of else if (weight > 2 || weight <= 6)
should be else if (weight > 2 && weight <= 6).

Another thing is that you could skip first checks in ifs.
Instead of else if (weight > 2 || weight <= 6)
can be else if( weight <= 6). In other ifs too.

And about your main question. Imo this code works correct (after earlier changes).
Can you specify your question? What is the result and what result you expect?
Thanks for the reply and also thanks for reminding me to change the || to &&.

The main question is that say the user inputs the weight of 4 and a distance of 700.
it should calculate the shipping in increments of 500. it would be 2.20 * 2. Total shipping would be 4.40
I believe it is using ceil but not sure exactly how to use it
Topic archived. No new replies allowed.