2

I have read several of the previously asked questions on this topic, but can't seem to find the answer I'm looking for. When I run the program I receive no errors, but I get a lot of garbage data. I know it's because I'm not passing the parameters right, but I'm new to c++ and specifically how to use pointers correctly. To make it simple: I am passing an employee object through PrintCheck(), which calls the CalcSalary() function, which must use GetHours() and GetWage() to access member data to do calculations and return the correct salary. Would appreciate any explanations as to why I am generating garbage data!

I have a class:

class Employee
{
private:
    int employeeNumber;
    string name;
    string streetAddress;
    string phoneNumber;
    double hourlyWage;
    double hoursWorkedperWeek;
public:
    Employee(void);
    Employee(int, string, string, string, double, double);
    int GetEmployeeNum() const;
    void SetEmployeeNum(int);
    string GetName() const;
    void SetName(string);
    string GetAddress() const;
    void SetAddress(string);
    string GetPhone() const;
    void SetPhone(string);
    double GetWage() const;
    void SetWage(double);
    double GetHours() const;
    void SetHours(double);
    double CalcPay(double, double);
};

I also have a function that needs to interact with the class:

void PrintCheck(Employee&);

My main function looks like:

void main()
{
    Employee joe(1, "Joe Blo", "125 Cool St.", "555 555 5555", 10.00, 45); //create employee 1
    Employee jane(2, "Jane Doe", "521 Dumb St.", "1 800 555 5555", 12.50, 30); //create employee 2
    PrintCheck(joe); //print check
}

The printcheck function looks like:

void PrintCheck(Employee& a)
{

    cout << "Pay to the order of " << a.GetName() << "...................................";
    cout << a.CalcPay(a.GetWage(), a.GetHours()) << endl;
    cout << "Hours worked: " << a.GetHours() << endl;
    cout << "Hourly wage: " << a.GetWage() << endl;
}

Calcpay function is:

double Employee::CalcPay(double h, double w)
{
    double salary = 0;
    int OT = 40;
    double timeandahalf = 1.5;
    double STATE = .075;
    double FED = .20;
    if (h > OT) // overtime
    {
        salary = h * (w * timeandahalf); // calc time and a half
        salary = salary * STATE; // calc state deductions
        salary = salary * FED; // calc federal deductions
    }
    else 
    {
        salary = h * w; // calc salary
        salary = salary * STATE; // calc state deductions
        salary = salary * FED; // calc federal deductions
    }
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(PRECISION);
    return salary;
}

My "get" functions all follow this pattern:

int Employee::GetEmployeeNum() const
{
    return employeeNumber;
}

My expected output would be:

Pay to the order of:  Joe Blo............ $salary.
Hours worked: $hours.
Hourly wage: $wage.

What I got:

Pay to the order of: ........................ 128509280503000000000000000000000000000.00 (this number is long, but I didn't feel like typing them all)
Hours worked: -9723636237 (same, just tons of bs numbers)
Hourly wage: (the exact same number as above)

My class constructor:

Employee::Employee(int en, string n, string a, string p, double w, double h)
{
    int employeeNumber = en;
    string name = n;
    string streetAddress = a;
    string phoneNumber = p;
    double hourlyWage = w;
    double hoursWorkedperWeek = h;
}
Employee::Employee()
{
}
7
  • 1
    Why don't you include what output you expected and what you actually got in your question? Commented Sep 16, 2013 at 21:21
  • 1
    What does the implementation for your constructor Employee(int, string, string, string, double, double) look like? Commented Sep 16, 2013 at 21:23
  • Watch your language. I'll edit it out for you this time, but in the future a mod can delete your post without even blinking. Commented Sep 16, 2013 at 21:25
  • Is your constructor properly init'ing the instance? Commented Sep 16, 2013 at 21:30
  • 1
    Sorry about language, you know how frustrating errors can be! I edited in my constructor at the bottom of the post. Commented Sep 16, 2013 at 21:35

2 Answers 2

4

Your Employee::Employee(int en, string n, string a, string p, double w, double h) is declaring new local variables inside it and therefore shadowing the member variables inside your class. So in reality, you never properly initialized any of its members during construction.

The following should correct that problem:

Employee::Employee(int en, string n, string a, string p, double w, double h)
  : employeeNumber ( en ),
    name ( n ),
    streetAddress ( a ),
    phoneNumber ( p ),
    hourlyWage ( w ),
    hoursWorkedperWeek ( h )
{
}
Sign up to request clarification or add additional context in comments.

8 Comments

This does not work. The compiler isn't recognizing any of the data members when I try this.
An build error says "found in what should be a comma-separated list". On each of the lines.
@xavi whoops, forgot to fix the commas during copy/paste, try now
Would you look at that! Works perfectly. Could you possibly explain why this syntax allows my data to work?
@xavi This is using an initializer list.
|
2

This is the error

Employee::Employee(int en, string n, string a, string p, double w, double h)
{
    int employeeNumber = en;
    string name = n;
    string streetAddress = a;
    string phoneNumber = p;
    double hourlyWage = w;
    double hoursWorkedperWeek = h;
}

it should be

Employee::Employee(int en, string n, string a, string p, double w, double h)
{
    employeeNumber = en;
    name = n;
    streetAddress = a;
    phoneNumber = p;
    hourlyWage = w;
    hoursWorkedperWeek = h;
}

or even better it should be as in greatwolf's answer.

The error in your version is that you've declared variables in your constructor with exactly the same names as the members of your class. These variables hide your class members, so your constructor does not initialise your class. So you get garbage.

2 Comments

Thank you for the help. I can't believe the problem was something so stupid and easily overlooked. Overall, my understanding is greater. Thanks again.
@xavi C++ is full of gotchas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.