Shipping Charges
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Shipping Charges

  1. #1
    Join Date
    Sep 2005
    Posts
    2

    Shipping Charges

    Hi,
    Please anybody can look at this program and tell me what is the problem that I can't get the validation to work

    The Fast Freight Shipping Company charges the following rates:

    Write a program that asks for the weight of the package and the distance it is to be shipped then displays the charges.

    Weight of Package (in kilograms) | Rate Per 500 Miles Shipped

    2 Kg or less $1.10

    Over 2 Kg but not more than 6 Kg $2.20

    Over 6 Kg but not more than 10 Kg $3.70

    Over 10 Kg but not more than 20 Kg $4.80

    Input Validation: Do not accept values of 0 or less for the weight of the package. Do not accept weights of more than 20Kg (this is the maximum weight the company will ship,). Do not accept distances of less than 10 miles or more than 3,000 miles. These are the company’s minimum and maximum shipping distances.


    //---------------------------------------------------------------------------
    #include <iostream.h>
    #include <iomanip>
    #include <cstring>
    #include <fstream>
    #include <utility>
    #include <conio.h>
    using namespace std;
    //---------------------------------------------------------------------------
    int main (void)
    {
    // Assuming Varibles
    double wt_package;
    double distance;
    double charges;
    double rate;

    // Asking for inputs.
    cout << "Please enter the Wt. Package: ";
    cin >> wt_package;
    cout << "Please enter the distance: ";
    cin >> distance;


    // Dertermining the price range.
    if (wt_package>0.0 || wt_package<20.0)
    // if (wt_package<=0.0){
    //cout<< "Please enter the wt. of package above 0.0";
    //}else
    if (wt_package>0.0 && wt_package<2.0){
    rate=1.10;
    }else
    if (wt_package>2.0 && wt_package<6.0){
    rate=2.20;
    }else
    if (wt_package>6.0 && wt_package<10.0){
    rate=3.70;
    }else
    if (wt_package>10.0 && wt_package<20.0){
    rate=4.80;
    }//else
    // if (wt_package>20.0){
    // cout<< "Please enter the wt. of package below or equal to 20.0";
    //}


    // Dertermining the price range.
    if (wt_package>10.0 || wt_package<3000.0)

    //cout<< "Please enter the distance above 10.0 and below or equal to 3000.0";

    if (distance>10.0 && distance<500.0){
    distance=1.0;
    }else
    if (distance>500.0 && distance<1000.0){
    distance=2.0;
    }else
    if (distance>1000.0 && distance<1500.0){
    distance=3.0;
    }else
    if (distance>1500.0 && distance<2000.0){
    distance=4.0;
    }else
    if (distance>2500.0 && distance<=3000.0){
    distance=5.0;
    }

    // calculating
    charges = wt_package * rate * distance;

    // Displaying the result.
    cout << "\nThe charges for shipping is: $" << charges << endl;
    cout << endl << endl << endl << "Press Any Key to Continue...";
    getch ();
    return 0;
    }

  2. #2
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    Re: Shipping Charges

    A few things. First of all, if you want to set up your program so that it keeps asking for user input until it gets a valid value, which is what I think of when I read "input validation," then you need to have some kind of looping mechanism wrapped around your input statements. Generally speaking, your program should probably be structured something like this:
    Code:
    do
    {
        Input package weight.
        if (package weight is invalid)
            Print error message.
    } while (package weight is invalid);
    
    do
    {
        Input shipping distance.
        if (shipping distance is invalid)
            Print error message.
    } while (shipping distance is invalid);
    
    Calculate shipping rate and print result.
    So you're on the right track; just rework what you've got a bit so it fits this pattern or something like it, and you should be there.

    Second, double-check what the problem says about how to compute the shipping rate. The rates such as $1.10, $2.20, etc. are the rate per 500 miles. So rate * distance is sufficient. Don't multiply it by wt_package as you're doing, or the results will come out far too high. Also, consider whether you might be able to determine the final value of distance mathematically, rather than using a block of if statements. It shouldn't be hard to figure out. I doubt you'll be docked points if you leave it as is, but it's a good exercise.

    Third, make sure that you're careful about when to use <= instead of <, and >= instead of >. As an example, your program as it's currently written won't know what to do if the user enters a weight that is exactly 2.0 pounds, because it falls right between two of your categories.

  3. #3
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Shipping Charges

    Debug your app and see which variable receives which value and you'll find out the errors. ( No cake for you until you finish your homework )
    Har Har

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center