简体   繁体   中英

Passing variables from one function to another trouble (C++)

I seem to be having trouble with passing variables from one function to another. My main class looks like this :

class EmployeeClass {
public:
    void ImplementCalculations(string EmployeeName, double hours, double wage);
    void DisplayEmployeeInformation();
    void Addsomethingup (EmployeeClass, EmployeeClass, EmployeeClass);
    string EmployeeName;
    double hours;
    double wage;
    double basepay;
    double overtime_hours;
    double overtime_pay ;
    double overtime_extra ;
    double iTotal_salaries ;
    double iIndividualSalary;
    double iTotal_hours ;
    double iTotal_OvertimeHours;
};

I am passing a string and two doubles to the function "ImplementCalculations" as shown here:

Employee1.ImplementCalculations (Employee1.EmployeeName, Employee1.hours, Employee1.wage);
    Employee2.ImplementCalculations (Employee2.EmployeeName, Employee2.hours, Employee2.wage);
    Employee3.ImplementCalculations (Employee3.EmployeeName, Employee3.hours, Employee3.wage);

These passed variables have some math done to them:

void EmployeeClass::ImplementCalculations (string EmployeeName, double hours, double wage) {
    //Initialize overtime variables
    double overtime_hours=0;
    double overtime_pay=0;
    double overtime_extra=0;
    double basepay = 0;
    double iIndividualSalary = 0;


    if (hours > 40) 
    {       
        basepay = 40 * wage;
        overtime_hours = hours - 40;
        overtime_pay = wage * 1.5;
        overtime_extra = overtime_hours * overtime_pay;
        iIndividualSalary = overtime_extra + basepay;

        /*
        Implement function call to output the employee information.  Function is defined below.
        */
        DisplayEmployeeInformation ();


    }   // if (hours <= 40)
    else
    {   
        basepay = hours * wage;
        overtime_hours=0;
        overtime_extra=0;
        iIndividualSalary = basepay;
        /*
        Implement function call to output the employee information.  Function is defined below.
        */
        DisplayEmployeeInformation();

    }; // End of the else

However this is where things go sour. I think that because I am not passing DisplayEmployeeInformation anything it doesn't want to work, which also makes it so that AddSomethingUp doesn't work as well.

void EmployeeClass::DisplayEmployeeInformation () {
    // This function displays all the employee output information.

    cout << "Employee Name ............. = " << EmployeeName << endl;
    cout << "Base Pay .................. = " << setprecision(5)<< basepay << endl;
    cout << "Hours in Overtime ......... = " << setprecision(4)<< overtime_hours << endl;
    cout << "Overtime Pay Amount........ = " << setprecision(5)<< overtime_pay << endl;
    cout << "Total Pay ................. = " << setprecision(6)<< iIndividualSalary << endl;


} // END OF Display Employee Information


void EmployeeClass::Addsomethingup (EmployeeClass Employee1, EmployeeClass Employee2, EmployeeClass Employee3){
    /* 
    Adds the total hours for objects 1, 2, and 3.
    Adds the salaries for each object.
    Adds the total overtime hours.
    */
    iTotal_hours = Employee1.hours + Employee2.hours + Employee3.hours;
    iTotal_salaries = Employee1.iIndividualSalary + Employee2.iIndividualSalary + Employee3.iIndividualSalary;
    iTotal_OvertimeHours = Employee1.overtime_hours + Employee2.overtime_hours + Employee3.overtime_hours;



    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%% Total Employee Salaries ..... = " << setprecision(6)<<iTotal_salaries <<endl;
    cout << "%%%% Total Employee Hours ........ = " << setprecision(5)<<iTotal_hours << endl;
    cout << "%%%% Total Overtime Hours......... = " << setprecision(5)<<iTotal_OvertimeHours << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;

} // End of function

The one line in AddSomethingUp that works is the iTotal_hours. No idea why it gets that, but it is the only thing it gets.

So my question is do I have to pass DisplayEmployeeInformation some sort of variables? What should that look like? More like passing the information to ImplementCalculations?

EmployeeClass has a number of member variables such as: hours, overtime_hours and others.

The ImplementCalculations function declares a bunch of local variables right at the start with the exact same name. It also has parameters with the exact same name.

I assume that in the start of ImplementCalculations you don't wish to declare new variables but rather set the values for the ones in the class:

void EmployeeClass::ImplementCalculations (string EmployeeName, double hours, double wage) {
    //Initialize overtime variables
    overtime_hours=0;
    overtime_pay=0;
    overtime_extra=0;
    basepay = 0;
    iIndividualSalary = 0;
    ... (other code here) ...

Also consider renaming the parameter variables to something like p_EmployeeName and p_hours (etc) so they don't clash with the member variable names in your class.

While it is not 100% clear what your problem is, it is very likely that the cause of it is the fact that you are not using references .

For example, take this function:

void Addsomethingup (EmployeeClass Employee1);

This creates a copy of Employee1 , which is thrown away when the function is left. The original object which you pass into the function is not touched and remains the same.

You might try this:

void Addsomethingup (EmployeeClass &Employee1);

You can also pass const references as an alternative to useless copying if you don't intend to modify the object:

void Addsomethingup (EmployeeClass const &Employee1);

In fact, you should do so for your std::string arguments (although C++11 may be kind of a game changer here, but you better consult Google for this, or else it becomes too off-topic for this question). And you probably need to read a good C++ book. References are a pretty basic language feature.

If you come from a Java background, don't be confused by the word "reference". A "reference" in Java is more a like a pointer in C++, whereas C++ references do not have a Java counterpart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM