Jump to content

shipping calculation outputting wrong

- - - - -

  • Please log in to reply
8 replies to this topic

#1
lva28

lva28

    Newbie

  • Members
  • Pip
  • 6 posts
I need assistance since it keeps outputting 8.00 when it should be different if the weight is over 50


float CalcShipping(int &totalWt,)

{

    if (totalWt <= 25)

  {

    return 8.00;

  }

  else if (totalWt <= 50)

  {

    return (totalWt - 25) *.15 + 8.00;

  }

  else if (totalWt > 50)

  {

    return (totalWt - 25) *.09 + 8.00;

  }

}





Edited by lva28, 01 December 2011 - 06:01 PM.


#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
I don't see anywhere where you've initialized totalWt. In C/C++, you need to initialize your variables or else they have unpredictable starting values based on whatever happens to be in memory before they're allocated.

Edit: To put it simply: Don't assume your variables start with a value of zero unless you explicitly initialize them as so.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
lva28

lva28

    Newbie

  • Members
  • Pip
  • 6 posts
Thanks for the tip I initialized it and it still always returns $5.00, did you see any other reasons why this is so?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You set totalWt to 0, then check to see if it's less than 25. It always will be.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
The problem is this line:
totalWt=0.00;
In your CalcShipping function, you initialize it and then the function returns $5 because 0 is smaller than 25.

Also, a small tip; you don't have to use break for each case in switch statements, if you want several cases to do the same thing.
case 'o':
    cout << "Orange County";
    break;
case 'O':
    cout << "Orange County";
    break;
So you can change that to this:
case 'o':
case 'O':
    cout << "Orange County";
    break;

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
lva28

lva28

    Newbie

  • Members
  • Pip
  • 6 posts
I initialized it after I posed the problem because the first responder told me to initialize totalWt to 0. Without initializing it, totalWt still always returns 5.00, is there another solution other then the initialization?

#7
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
You should initialize it in main function, not in CalcShipping.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#8
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Initialize it when you declare it, before you pass it to any functions.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#9
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
1. Why is the parameter a reference? I don't think it's necessary, since you are not doing anything to it. You are just reading it.
2. As a programming practice, the last (else if ) is not necessary. It can simply be an else, since everything else is covered.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users