1

I cannot explain why I am getting this logic error! Take a look:

This is in the header -

class PayRoller
{
public:
    void initialize();

    double getNum();
    void setNum(double);
    double getGrossPay();
    void setGrossPay(double);
    double getWage();
    void setWage(double);
    double getAddTotal();
    void setAddTotal(double);
}

And here is the first function that is called after the object is made-

void PayRoller::initialize();
{
    setGrossPay(0.0);
    setWage(0.0);
    setAddTotal(0.0);
    cout << (getGrossPay() + getAddTotal());
    start();
}

And finally here are the getters and setters-

void PayRoller::setGrossPay(double temp)
{
grossPay = temp;
}

double PayRoller::getWage()
{
return wage;
}

void PayRoller::setWage(double temp)
{
wage = temp;
}

double PayRoller::getAddTotal()
{
return addTotal;
}

void PayRoller::setAddTotal(double temp)
{
wage = temp;
}

When I start the code (without debugging) the value I get from the cout in initialize() is -9.25596e+061

What am I doing wrong here? I can't seem to figure it out. Thanks in advance!

  • 1
    Where is `grossPay` even defined? – nneonneo Mar 01 '13 at 06:16
  • 1
    where is getGrossPay()? why do you use initialize() instead of constructor? – Slava Mar 01 '13 at 06:19
  • Is `addTotal` initialized in your constructor? C++ does not initialize members by default, so it's likely that this, or other, members are returning garbage due to uninitialized memory. – Richard Cook Mar 01 '13 at 06:19
  • 1
    Your `setAddTotal` function sets the **`wage`** , not `addTotal`... – DCoder Mar 01 '13 at 06:20
  • Hey. You've not defined getGrossPay(), at least in the question. Please edit your question. Also, where are all the variables that you're accessing defined in the class? Are they private members? Also, just pointing out - your setWage() and setAddTotal() functions do the same job. Is that intentional? – Sidd Mar 01 '13 at 06:20
  • 1
    Ugh I can't believe I missed that! DCoder was right and thats what fixed it. Thank you DCoder! And if anyone else would like to see my whole project just me know - hopefully that would explain it a bit better. Thank you again DCoder – Jacob Thompson Mar 01 '13 at 06:26
  • 1
    BTW - Money is best represented by integers not doubles. With doubles you will get rounding errors. – Ed Heal Mar 01 '13 at 06:32

3 Answers3

1

Your setAddTotal Method is not setting addTotal, its setting wage.

void PayRoller::setAddTotal(double temp)
{
    wage = temp;
}

should be

void PayRoller::setAddTotal(double temp)
{
    addTotal = temp;
}

Otherwise, addTotal isn't actually set to 0, it just contains garbage.

DanChianucci
  • 1,175
  • 2
  • 11
  • 21
0

You need to scrap the initialize() method and use a member initialization list.

What you call as Initialization is actually Assignment, Initialization can be done only in the member initialization list. What you need is initialization, ie: tying up a value to an member at the time of creation not assigning value to the member after creation.

Problem with Assignment approach is that you need to rely on the user of your class to call the methods in your defined way, which they may not.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Agreed of course, but there are two lines of code in `initialize()` which can't be called from an initialization list. Forget the call to `cout`, but I'm assuming `start` is required, and he may not want to call that in a constructor body. – Ed S. Mar 01 '13 at 06:29
  • Maybe this will help explain my code better https://www.dropbox.com/sh/kb0lcw9sda9ggje/zCQ3j84u0f – Jacob Thompson Mar 01 '13 at 06:31
  • @EdS.: Well it is a bad design to do so, and unless OP tells us what `start()` does, We are in no position to tell how to improve the same. – Alok Save Mar 01 '13 at 06:32
  • Eh... sometimes you need it. For example, a machine I'm working on right now defines a bunch of interface types. The implementations need to have parameterless constructors so that they can be instantiated under the covers, so they need an `Initialize` method to get the data they need to do their job. Also, sometimes (again, speaking in terms of hardware interfaces) initialization means moving mechanical parts around. You want control over when that is done, but sometimes you do need to instantiate the instances separately. There are always exceptions – Ed S. Mar 01 '13 at 06:37
0

Seems precesion issue number is almost 0. Try changing the values to long or int and this can be verfied.

Adnan Akbar
  • 718
  • 6
  • 16
  • For this problem I cannot use ints because the program requires two decimal places for dollar amounts but I got it working! Just accidentally had SetAddTotal() set the wrong variable. – Jacob Thompson Mar 01 '13 at 06:31
  • It was just to verify if precision was an issue not to change the datatype all together. – Adnan Akbar Mar 01 '13 at 06:34