Shipping Charge

My professor wants us to write a code for shipping prices:
Assignment 4 – Selection (if else)

Shipping Calculator: Global Courier Services will ship your package based on how much it weighs and how far you are sending the package. Packages above 50 pounds will not be shipped. You need to write a program in C that calculates the shipping charge.
The shipping rates are based on per 500 miles shipped. They are not pro-rated, i.e., 600 miles is the same rate as 900 miles or 1000 miles.

Here are the shipping charges -
Package Weight Rate per 500 miles shipped
• Less than or equal to 10 pounds $3.00
• More than 10 pounds but less than or equal to 50 pounds $5.00

If the shipping distance is more than 1000 miles, there is an additional charge of $10 per package shipped.

Test Case 1:
Input Data:
Weight: 1.5 pounds
Miles: 200 miles (This is one 500 mile segment.)
Expected results:
Your shipping charge is $3.00

This is what I have:
For some reason, it is not calculating. It stops at scanf("%lf", &distance);

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

main () {
double weight, shippingCharge, rate, segments;
int distance;
printf("Enter the weight: \n");
scanf("%lf", &weight);
printf("Enter the distance: \n");
scanf("%i", &distance);

if (weight <= 10) {
printf("Rate is $3.00 \n");
rate = 3;
} else {
printf("Rate is $5.00 \n");
rate = 5;
}

if (distance % 500 == 0) {
segments = distance / 500;
} else {
segments = distance / 500 + 1;
}


shippingCharge = rate * segments;

if (distance >1000) {
shippingCharge = shippingCharge + 10;
}

printf("Your shipping charge is $%lf\n", shippingCharge);

system ("pause");

}
Last edited on
Please use code tags when posting code. Highlight your code and click the <> button to the right of the edit window.
scanf("%lf", &distance);
You're telling scanf that you're scanning a double, but distance is an int.
printf("Your shipping charge is $%i\n", shippingCharge);
You're telling printf that your printing an integer, but shippingCharge is a double.

This is exactly why you shouldn't use scanf and printf. It's too easy to have the format string and the actual parameters not match up. It's much safer to use C++ streams instead.
My professor wants us to use scanf and printf. I'm trying to figure out how to get it to run. Help please
I've changed the distance to i and the shipping charge to lf, but it still won't run
Your code works for me on the test case that you provided. Can you describe what it's doing wrong for you?

BTW, one thing you still need to add is the part about not shipping packages that weigh more than 50 lbs.
Topic archived. No new replies allowed.