Thread: Shipping calculator problem

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    33

    Shipping calculator problem

    So im trying to create a program that will calculate a shipping cost. I'm stuck on trying to calculate the actual milage and cost.
    Example being 1400 miles for a 2lb package would be $2 for every 500miles. so it would cost $6. But Im having trouble getting the program to round up the 1400 to 1500 so it charges the correct amount.

    I know there would have to be a way to do this without programming all the conditions.

    Also the course has not reached loops yet.

    Code:
    // This program will calculate the shipping charges.// 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.
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    main() 
    {
        double packageWeight, packageDistance, packagePrice;
        int packageDistanceMath;
    
    
        printf("Enter the weight of the package: \n");
        scanf_s("%lf", &packageWeight);
        printf("The weight you have entered is %.2lf\n", packageWeight);
    
    
    // A package that weighs less than 10lbs will cost $2
        if (packageWeight <= 10 )
            packagePrice = 2.00;
    // A package that weighs less than or equal to 50lbs but more than 10lbs will cost $4.50
        if ( packageWeight <= 50 && packageWeight > 10 ) 
                packagePrice = 4.50;
    // Do not ship packages weighing more than 50lbs
        if (50 <= packageWeight){
                printf("The package will not ship.  \n");
                system("pause");
                }
    
    
        printf("How far are you sending the package? \n");
        scanf_s("%lf", &packageDistance);
        printf("The distance you entered is %.2lf\n", packageDistance);   
    
    
    // 200 miles will cost the same as 500 miles
    // The milage is not pro-rated
        if (packageDistance < 500){
            packageDistance = 1;
            packagePrice = packageDistance * packagePrice;
            printf("The shipping charge is %.2lf \n", packagePrice);
            system("pause");
            }
    
    
        packageDistanceMath = (packageDistance/500);
        packagePrice = packageDistanceMath * packagePrice;
        
        printf("The shipping charge is %.2lf \n", packagePrice);
        system("PAUSE");
    }

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Here are some things you can try:

    1. Read in packageDistance as an integer and do integer division. You can rely on the fact that integer division truncates and use a nifty trick to always round the result up. Add 499 to packageDistance before dividing.
    Code:
    (1000 + 499) / 500 = 2
    (1001 + 499) / 500 = 3
    2. If you must use floating point math you can try something like this:
    Code:
    int packageDistanceMath = (int)(packageDistance / 500.0);
    if (packageDistanceMath * 500.0 < packageDistance) {
       // round up if the packageDistance wasn't evenly divisible by 500
       packageDistanceMath++;
    }
    but I must warn you that floating point math is a bit trickier than integer math. The above code might not work for all floating point values due to minor inaccuracies in how decimals are represented in a binary format. Hopefully your teacher will cover the dangers of floating point math at some point.

    3. You can attempt to use some of the functions defined in math.h to help you out. For example, ceil() will round any floating point value up to the "smallest integer that is not less than" the value passed in.
    Code:
    int packageDistanceMath = (int)ceil(packageDistance / 500.0);
    I would recommend if you are a beginner to stick with integer math unless floating point is absolutely necessary. However, I certainly don't want you to get a bad grade if your teacher told you to use doubles and you followed my recommendation to use integers.

    Also, I wouldn't be afraid to ask your teacher what they recommend.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WTF! USPS Shipping...
    By holden in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-22-2004, 10:48 AM
  2. Best shipping message ever
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 06-21-2004, 08:11 AM
  3. Need help in designing Shipping Company!
    By VtecGSR in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2002, 12:03 PM