CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2011
    Posts
    4

    output for calculating shipping

    Hi I have been at this for hours and cannot understand why the shipping keeps outputting the same amount no matter which weight I enter. It is supposed to calculate the shipping based on the weight and there are three different options like if it is over 45 pounds it will be a different amount. Can anyone tell me what I need to fix?
    Code:
    float CalcShip(int &totalAt)
    {
     float ship;
     ship=0.00;
        if (totalAt <= 15)
      {
       ship= 8.00;
      }
      else if (totalAt <= 40)
      {
        ship= (totalAt - 15) *.1 + 8.00;
      }
      else if (totalAt > 50)
      {
        ship= (totalAt - 45) *.07 + 8.00;
      }
     return (ship);
    }
    Last edited by lva28; Tod
    Last edited by solar23; December 1st, 2011 at 10:09 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: output for calculating shipping

    Quote Originally Posted by solar23 View Post
    Hi I have been at this for hours and cannot understand why the shipping keeps outputting $5.00 no matter what weight I enter.
    So you've used your compiler's debugger? If not, please do so, as using the debugger that comes with your compiler is one of tools you must learn to use to debug your programs.

    Single step through the program, watch variables, see how the program flows, etc. Then you will know when the program does something unexpected and know how to fix the issue.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: output for calculating shipping

    Quote Originally Posted by solar23 View Post
    Hi I have been at this for hours and cannot understand why the shipping keeps outputting $5.00 no matter what weight I enter.
    Before you do anything, there are some issues with the code you have now.

    Why are you passing items by reference when they are not changed in the function? For example:
    Code:
    float CalcShipping(int &totalWt)
    Why is totalWt passed by reference?
    Code:
    float CalcShipping(int totalWt)
    The reason why it shouldn't be passed by reference is two-fold:

    1) The CalcShipping() function does not change totalWt in any way. Since it doesn't change it, and totalWt is a "simple" type (an int), it should be passed by value, not reference.

    2) Passing something by reference when it isn't meant to be can lead you into trouble if you did happen to (accidentally) change totalWt, changing the value of your passed-in items when the function returns. When you pass by value, this cannot happen since a temporary is created in the function, so it doesn't affect the original value.

    I wouldn't be surprised if 2) is the reason for your problems, as you do this in so many other places (passing by reference).

    The one thing you should pass by reference (or const reference) are "big" or expensive items such as entire objects. But since it's an int you're dealing with (and floats), and they are not meant to be changed within the function you're calling, then pass these items by value.

    You also have code duplication:
    Code:
      switch (cCode)
      {
        case 'o':
          cout << "Orange County";
          break;
        case 'O':
          cout << "Orange County";
          break;
        case 's':
          cout << "San Diego county";
          break;
        case 'S':
          cout << "San Diego county";
          break;
        case 'l':
          cout << "LA County";
          break;
        case 'L':
          cout << "LA County";
          break;
        default:
          cout << "Invalid input";
          break;
      }
    Here is a shorter version:
    Code:
    #include <ctype.h>
    //...
      switch (tolower(cCode))
      {
        case 'o':
          cout << "Orange County";
          break;
        case 's':
          cout << "San Diego county";
          break;
        case 'l':
          cout << "LA County";
          break;
        default:
          cout << "Invalid input";
          break;
      }
    Half the code is now eliminated. With more "tricks", this code could be reduced to only 3 or so lines, but I won't do that here.

    Regards,

    Paul McKenzie

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