trying to calculate cost of shipping

I just started a very basic programming class and I'm trying to figure out how to calculate the cost of shipping a package based on miles and weight. I can enter the weight and miles but then cost comes out to 0.00 every time. Not sure where I'm going wrong. Also I am not doing Loops yet.

#include <stdio.h>
#include <stdlib.h>

main(){
double Weight, Miles, Cost;

printf("Enter your Package Weight: \n");
scanf("%lf", &Weight);
printf("Enter Miles traveled: \n");
scanf("%lf", &Miles);

if (Weight <= 2)
Cost = (Miles/500) * 1.5;

if (Weight <= 6)
Cost = (Miles/500) * 3.7;

if (Weight <= 10)
Cost = (Miles/500) * 5.25;
else
printf(" Your package is overweight limit!, \n");



printf("Your total Cost is: %.2lf\n", &Cost);

system("pause");
}

At the:
1
2
3
4
5
6
7
8
if (Weight <= 2) 
Cost = (Miles/500) * 1.5;

if (Weight <= 6)
Cost = (Miles/500) * 3.7;

if (Weight <= 10)
Cost = (Miles/500) * 5.25;


Make sure after the first 'if' statement you change the next to 'else if'. Else it causes problems!

This is how it should've be:
1
2
3
4
5
6
7
8
9
10
if (Weight <= 2) 
Cost = (Miles/500) * 1.5;

else if (Weight <= 6)
Cost = (Miles/500) * 3.7;

else if (Weight <= 10)
Cost = (Miles/500) * 5.25;
else 
printf(" Your package is overweight limit!, \n");


Also.
(If weight <= 6) & (if weight <= 10). Makes no sense.

This is how I would write the program in general:

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

int main(){

double weight, miles, cost;

cout << "Enter your package weight: "; cin >> weight;
cout << "Enter miles traveled: "; cin >> miles;

    if(weight <= 2){
    cost = (miles/500) * 1.5;
    cout << "Your total price is: $" << cost;
    }
    else if(weight > 2 && weight <= 6){
    cost = (miles/500) * 3.7;
    cout << "Your total price is: $" << cost;
    }
    else if(weight > 6 && weight <= 10){
    cost = (miles/500) * 5.25;
    cout << "Your total price is: $" << cost;
    }
    else
    cout << "Your package is over the weight limit!" << endl;
}


EDIT: I recommend looking into structures & for loops. Great for dealing & solving these problems.

There is alot of better ways to deal with this problem than if/else statements.
Last edited on
Topic archived. No new replies allowed.