I need help with one function in my code....I think

This is a discussion on I need help with one function in my code....I think within the C++ Programming forums, part of the General Programming Boards category; Ok so I've been working on this code for a couple days, and I just have one little problem with ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Exclamation I need help with one function in my code....I think

    Ok so I've been working on this code for a couple days, and I just have one little problem with computing the pay. Whenever the DisplayPay function outputs the pay for the employee I get all zeros. I was hoping somebody could help me with my problem. I'll attach my code. Thanks for the help.
    Attached Files Attached Files

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to transfer the result of the calculation from your ComputePay to DisplayPay. As it stands right now, you have some local variables in the functions ProcessManufacturingWorker() and ProcessOfficeWorker(), that are not the same variable as in the functions that calculate pay. So you print the values that haven't been calculated.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    What do I need to do to fix that. I've tried to change the variables around so many times. This program is sucking the life out of me.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since it's written in C++, I would pass in the relevant result variables as references to the calculation routine (and remove the local ones in the calculation routine, of course).

    If this makes no sense to you, check the Tutorial here on "references".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Is what I did with my code correct. That tutorial was pretty brief.
    Attached Files Attached Files

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Does it work? I don't think it's quite right, becaus you still have local variables for
    Code:
    	double overtimePay = 0;
    	double bonusPay = 0;
    inside CalculateHourlyPay() - and you need those to be available in the printing function, don't you?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    When I execute the program and I enter the hours worked and hourly rate when I get the output the numbers are like -92360000000000000000000000 for some reason.

    I'll attach my code as it is now.
    Attached Files Attached Files

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You put references in the wrong place.

    References mean that you are "transferring an alias rather than a copy" of a variable.

    This in turn means that whatever you pass in can be changed, and the resulting change appears in the calling code.

    If you just pass in a regular variable (without a reference) you get a copy in the called function (aka callee), and changes are only made to the callee's copy, not the "original" variable.

    So in this case, you need to pass in the values (as references) you want calculated by the calculation function, so that you can pass the calculated result to the display function. Currently, you calculate a local value, and then throw it away again.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    So do I need to put the & operator after my variables in the computehourlypay function?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by NicAuf View Post
    So do I need to put the & operator after my variables in the computehourlypay function?
    No, you need to REMOVE the local variables, there and pass them (with the & sign, but it's a storage modifier at this point, not really an operator).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Like this.....
    Attached Files Attached Files

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not really - you still don't pass the all the components to compute the pay to the function, do you?

    Does the code you just posted compile? [My "brain-compile" says that you should have about four errors for "undefined variabe" in the CalculateHourlyPay function].

    Try to make your code compile before you post it - it sorts out some of the more basic bits missing. Also try to re-read my previous post.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Ok well I've been working on that for an hour and I still can't get it to work. Help anybody!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21