shipping program

I'm writing a simple shipping program where a package weighing a certain amount and traveling per 500 miles = cost.

For example, a package weighing 2kg traveling up to 500 miles costs $1.10
if it exceeds 500 miles then it goes up another $1.10, same for if it goes up another per 500 miles.

If you ship a package 400 miles and it weighs 2 kg, the bill should be $1.10. If you ship a package 700 miles and it weighs 2.5 kg, it should cost $4.40.

My question is, is there a way to to write the code without writing multiple if else statements

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
#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, const char * argv[]) {
    
    double weight, miles, cost;
    
    cout << "Enter your package weight: "; cin >> weight;
    cout << "Enter miles traveled: "; cin >> miles;
    
    if(weight <= 2){
        cost = (miles/500) * 1.10;
        cout <<setprecision(2)<<fixed << "Your total price is: $" << cost;
    }
    else if(weight > 2 && weight <= 6){
        cost = (miles/500) * 2.20;
        cout<<setprecision(2)<<fixed << "Your total price is: $" << cost;
    }
    else if(weight > 6 && weight <= 10){
        cost = (miles/500) * 3.70;
        cout <<setprecision(2)<<fixed << "Your total price is: $" << cost;
    }
    else if(weight > 10 && weight <= 20){
        cost = (miles/500) * 4.80;
        cout <<setprecision(2)<<fixed << "Your total price is: $" << cost;
    }
    else
        cout << "Your package is over the weight limit!" << endl;
Last edited on
yes.
you can convert weight to a category and use a switch statement. This is probably not a good fit for you, but you asked.

chained if/else know the previous.
that is, if its <=2 else {here, you know it isn't <= 2, don't test again}

you can also exploit integer math and array indices to totally eliminate the mess.

double looky[] = {1.1,1.1,1.1, //0,1,2
2.2,2.2,2.2,2.2, //3,4,5,6
3.7, .... keep going //7,8,9,10 ...
//11-20
}

and do this:
if(weight > 20)
cout << "bad user" << endl;
else
cost = looky[(int)weight]*miles/500;

note that (int)weight is 2 for 2, 1 for 1.9 or 1.1, 0 for 0.5, etc... see how the indexing works?

this is cute, efficient, and very clean for small lists of values. For very large lists, it is not so good, for example if they allowed up to 10 tons in increments of 5... you can use loops to populate the table but at some point its just as ugly as anything else. It also works because you have continuous values from 0-20. If you had 0-10 and 15-20 with a gap, you have to get weird and need at least 1 more if statement.

If you had a huge list of categories you would probably want to revisit the ideas and figure out some clean way to do it, but it depends on the data what approach might be best.

Last edited on
I have no idea what you wrote lol. i haven't learned switch statements yet
Last edited on
sorry.
the answer is yes, anyway.

try the bottom solution, see for yourself. Everything from line if(weight <= 2) to the end can be replaced with this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
double looky[] = {1.1,1.1,1.1, //0,1,2
2.2,2.2,2.2,2.2, //3,4,5,6
3.7, 3.7,3.7,3.7, //7-10
4.8,4.8,4.8,4.8,4.8,4.8,4.8,4.8,4.8,4.8//11-20
};


if(weight > 20) 
cout << "Your package is over the weight limit!" << endl; 
else
{
cost = looky[(int)weight]*miles/500;
cout <<setprecision(2)<<fixed << "Your total price is: $" << cost;
}


I am not saying it is better. I am just showing you an answer to your question.
Last edited on
You're using floating point arithmetic here:
 
cost = (miles/500) * 1.10;

Consider if the distance is 250 miles. Your cost will be $0.55. That's not what the problem specification is asking for.. You want to use integer arithmetic.
1
2
3
  int multiplier;
  multiplier = (int)miles / 500;
  cost = (multiplier+1) * 1.10;


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
abstractionAnon, that is exactly what I needed. Thank you very much
Topic archived. No new replies allowed.