CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Oct 2017
    Posts
    6

    [RESOLVED] shipping calculator help

    Here is the assignment

    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 $2.00
    • More than 10 pounds but less than or equal to 50 pounds $4.50

    Test Case 1:
    Input Data:

    Weight: 1.5 pounds
    Miles: 200 miles (This is one 500 mile segment.)


    So I googled and tried everything for the past 6 hours and nothing really helped. Every per 500 miles it is pro rated. I've tried subtracting 1 from 500 and dividing by 500 and I still don't get the test case results.

    Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    
    	
    	double weight, rate, shippingCharge, miles;
    
    	// User enters the weight and mileage
    	printf("Enter the weight of the package:");
    	scanf("%lf", &weight);
    	printf("Enter the amount of miles it would take:");
    	scanf("%lf", &miles);
    	
    // When weight is greater than 50
    if(weight > 50) {
    	printf("Sorry, we only ship packages of 50 pounds or less. \n");
    	}
    	else
    	// When weight is less than 10
    	if(weight < 10) {
    		rate = 2.00;
    		shippingCharge = ((miles + 499) / 500 ) * rate;
    		printf("Shipping charge is $%.2lf \n", shippingCharge);
    	}
    	// When weight is greater than 10
    	else
    	if(weight > 10) {
    		rate = 4.50;
    		shippingCharge = ((miles + 499) / 500) * rate;;
    		printf("Shipping charge is $%.2lf \n", shippingCharge);
    	}
    	// When weight is less than or equal to 50
    	else
    	if(weight <= 50) {
    		rate = 4.50;
    		shippingCharge = ((miles + 499) / 500) * rate;
    		printf("Shipping charge is $%.2lf \n", shippingCharge);	
    	}
    	
    	system("pause");	
    }
    It just does not work....any suggestions?

    PS: I tried the ceil function and it did not work. Perhaps I did it wrong?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: shipping calculator help

    Code:
    shippingCharge = ((miles + 499) / 500 ) * rate;
    close. If we declare an int for the multiple value of the shipping miles to be used for the calculation (ie 1 for the first 500, 2 for 1000 etc) then consider

    Code:
    int multiple = int((miles + 499) / 500);
    As miles is a double, you need to just use the integer part! Then multiply this by the rate to get the shipping charge.

    Also, as the only change for the different weight is the rate to be used, within the if statements just set rate and then at the end of them have the calculation and the display.

    Also note that your if conditions are not all needed. You already check for > 50, so you then need just to check for < 10. If not < 10 (the else part of the if), then it must be greater than 10 and <= 50. So you only need 1 if statement to set rate to either 2 or 4.5.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Oct 2017
    Posts
    6

    Re: shipping calculator help

    Quote Originally Posted by 2kaud View Post
    Code:
    shippingCharge = ((miles + 499) / 500 ) * rate;
    close. If we declare an int for the multiple value of the shipping miles to be used for the calculation (ie 1 for the first 500, 2 for 1000 etc) then consider

    Code:
    int multiple = int((miles + 499) / 500);
    As miles is a double, you need to just use the integer part! Then multiply this by the rate to get the shipping charge.

    Also, as the only change for the different weight is the rate to be used, within the if statements just set rate and then at the end of them have the calculation and the display.

    Also note that your if conditions are not all needed. You already check for > 50, so you then need just to check for < 10. If not < 10 (the else part of the if), then it must be greater than 10 and <= 50. So you only need 1 if statement to set rate to either 2 or 4.5.
    I am sorry, I am not sure if I follow.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    
    	
    	double weight, rate, shippingCharge, miles, multiple;
    
    	// User enters the weight and mileage
    	printf("Enter the weight of the package:");
    	scanf("%lf", &weight);
    	printf("Enter the amount of miles it would take:");
    	scanf("%lf", &miles);
    	
    // When weight is greater than 50
    if(weight > 50) {
    	printf("Sorry, we only ship packages of 50 pounds or less. \n");
    	}
    	else
    	// When weight is less than 10
    	if(weight < 10) {
    		rate = 2.00;
    		int multiple = int((miles + 499) / 500) * rate;
    			result = shippingCharge;
    		printf("Shipping charge is $%.2lf \n", shippingCharge);
    	}
    	// When weight is greater than 10
    	else
    	if(weight > 10) {
    		rate = 4.50;
    		int multiple = int((miles + 499) / 500) * rate;
    			result = shippingCharge;
    		printf("Shipping charge is $%.2lf \n", shippingCharge);
    	}
    	// When weight is less than or equal to 50
    	else
    	if(weight <= 50) {
    		rate = 4.50;
    		int multiple = int((miles + 499) / 500) * rate;
    			result = shippingCharge;
    		printf("Shipping charge is $%.2lf \n", shippingCharge);	
    	}
    	
    	system("pause");	
    }
    I am confused to where you mean to insert that expression. I tried this, but got compile errors.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: shipping calculator help

    Code:
    int multiple = int((miles + 499) / 500) * rate;
    result = shippingCharge;
    becomes

    Code:
    int multiple = int((miles + 499) / 500);
    shippingCharge = multiple * rate;
    or shorter

    Code:
    shippingCharge = int((miles + 499) / 500) * rate;
    The only issue with the original code calculation in post #1 is that the result of (miles + 499) / 500 needs to be truncated to an int as miles is a double. So if miles is say 200.0 then (200 + 499) / 500 = 1.398 - but you need this to be 1. So use int() to obtain the integer part - 1.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Oct 2017
    Posts
    6

    Re: shipping calculator help

    Quote Originally Posted by 2kaud View Post
    Code:
    int multiple = int((miles + 499) / 500) * rate;
    result = shippingCharge;
    becomes

    Code:
    int multiple = int((miles + 499) / 500);
    shippingCharge = multiple * rate;
    or shorter

    Code:
    shippingCharge = int((miles + 499) / 500) * rate;
    The only issue with the original code calculation in post #1 is that the result of (miles + 499) / 500 needs to be truncated to an int as miles is a double. So if miles is say 200.0 then (200 + 499) / 500 = 1.398 - but you need this to be 1. So use int() to obtain the integer part - 1.
    Ah I get it now! Thank you so much

  6. #6
    Join Date
    Oct 2017
    Posts
    6

    Re: shipping calculator help

    I'm sorry, another question. When I put this in, I still get a compile time error. It says: [Error] Expected expression before int...


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    
    	int multiple, miles;
    	double weight, rate, shippingCharge;
    
    	// User enters the weight and mileage
    	printf("Enter the weight of the package:");
    	scanf("%lf", &weight);
    	printf("Enter the amount of miles it would take:");
    	scanf("%lf", &miles);
    	
    // When weight is greater than 50
    if(weight > 50) {
    	printf("Sorry, we only ship packages of 50 pounds or less. \n");
    	}
    	else
    	// When weight is less than 10
    	if(weight < 10) {
    		rate = 2.00;
    		int multiple = int((miles + 499) / 500);
    		shippingCharge = multiple * rate;
    		printf("Shipping charge is $%.2lf \n", shippingCharge);
    	}
    	// When weight is greater than 10
    	else
    	if(weight > 10) {
    		rate = 4.50;
    		int multiple = int((miles + 499) / 500);
    		shippingCharge = multiple * rate;
    		printf("Shipping charge is $%.2lf \n", shippingCharge);
    	}
    	// When weight is less than or equal to 50
    	else
    	if(weight <= 50) {
    		rate = 4.50;
    		int multiple = int((miles + 499) / 500);
    		shippingCharge = multiple * rate;
    		printf("Shipping charge is $%.2lf \n", shippingCharge);	
    	}
    	
    	system("pause");	
    }
    Last edited by Aelos; October 9th, 2017 at 12:24 PM.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: shipping calculator help

    Code:
    if (weight <= 10) {
    	rate = 2.00;
    	shippingCharge = int((miles + 499) / 500) * rate;
    	printf("Shipping charge is $%.2lf \n", shippingCharge);
    }
    This is for one calculation, the others are similar. This compiles OK for me with VS2017. If you are still getting compile errors, then post your revised code.
    Last edited by 2kaud; October 9th, 2017 at 03:56 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: shipping calculator help

    Sorry. I forgot you are using c and not c++.

    In c you need brackets around the int

    Code:
    if (weight <= 10) {
    	rate = 2.00;
    	shippingCharge = (int)((miles + 499) / 500) * rate;
    	printf("Shipping charge is $%.2lf \n", shippingCharge);
    }
    Last edited by 2kaud; October 9th, 2017 at 03:56 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Oct 2017
    Posts
    6

    Re: shipping calculator help

    Thanks again, you are a lifesaver )

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: shipping calculator help

    Note that your code in post #6 can be shortened. You only need 1 if test and one printf statement. Consider

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	double weight, miles, rate;
    
    	// User enters the weight
    	printf("Enter the weight of the package:");
    	scanf("%lf", &weight);
    
    	// When weight is greater than 50
    	if (weight > 50.0) {
    		puts("Sorry, we only ship packages of 50 pounds or less.");
    		return 0;
    	}
    
    	// User enters the mileage
    	printf("Enter the amount of miles it would take:");
    	scanf("%lf", &miles);
    
    	// When weight is less than or equal 10
    	if (weight <= 10.0)
    		rate = 2.00;
    
    	// When weight is greater than 10
    	else
    		rate = 4.50;
    
    	// Calculate and display the result
    	printf("Shipping charge is $%.2lf \n", (int)((miles + 499.0) / 500.0) * rate);
    
    	system("pause");
    }
    Last edited by 2kaud; October 9th, 2017 at 03:55 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    Oct 2017
    Posts
    6

    Re: shipping calculator help

    Looks 10x less complicated. Thanks for shortening it up

  12. #12
    Join Date
    Oct 2019
    Posts
    1

    Re: shipping calculator help

    Quote Originally Posted by Aelos View Post
    Looks 10x less complicated. Thanks for shortening it up

    If you try this formal for other test cases it gives out the wrong answers

    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

    Test Case 2:

    Input Data:

    Weight: 5.6 pounds

    Miles: 1200 miles (This is three 500 mile segments.)

    Expected results:

    Your shipping charge is $19.00

    Test Case 3:

    Input Data:

    Weight: 11.0 pounds

    Miles: 2000 miles (This is four 500 mile segments.)

    Expected results:

    Your shipping charge is $30.00

    Test Case 4:

    Input Data:

    Weight: 38 pounds

    Miles: 9600 miles (This is twenty 500 mile segments.)

    Expected results:

    Your shipping charge is $110.00

    Test Case 5:

    Weight: 55 pounds

    Miles: 345 miles

    Expected results:

    Sorry, we only ship packages of 50 pounds or less.

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: [RESOLVED] shipping calculator help

    ... and the issue in the code is ?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Nov 2019
    Posts
    2

    Re: [RESOLVED] shipping calculator help

    Quote Originally Posted by 2kaud View Post
    ... and the issue in the code is ?
    Basically when compiled, the shipping charge comes out to $0 for any numbers inputted.

  15. #15
    Join Date
    Nov 2019
    Posts
    2

    Re: [RESOLVED] shipping calculator help

    Quote Originally Posted by noobcodezz View Post
    Basically when compiled, the shipping charge comes out to $0 for any numbers inputted.
    Nevermind, that was the original code. Not the corrected one. Sorry about that

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured