Class - wierd 'salary.displayPay(pay)' output?

Hi all,

To start, I am beginner in C++ (2nd week), and we're learning about classes and all that goes with it. I'm not even sure that my functions are even being used correctly. But, that's not my issue at the moment.

I am concerned with the last function in my Main.cpp file that displays a non-sensical numeric value when the user inputs the required information to calculate their paycheck. The crazy value is: $9.22337e+016.

Can anybody offer an explanation or point me in the right direction into solving this? It's due today so I'm getting a little nervous that it won't work correctly before submission.

Here are my files:

Salary.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
                           //************************************
                           //  EmployeeSalary class definitions
                           //************************************

#ifndef salary_h
#define salary_h
#include <iostream>
#include <string>


using namespace std;

class EmployeeSalary
{
private:
        string name;
        float HrlyWage;
	int Hrs;

public:
        EmployeeSalary();               //Constructor - enables the initialization of variables of the class
        ~EmployeeSalary();              //Destructor - cleanup members of the class that need cleanup
       	int getHrs(int);
        float getHrlyWage(float);
        float displayPay(float);
};

#endif 


Salary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
                           //************************************
                           //  EmployeeSalary method definitions
                           //************************************


#include "salary.h"
#include <iostream>
#include <string>




EmployeeSalary::EmployeeSalary()             //Constructor to enable variable access
{
}


EmployeeSalary::~EmployeeSalary()            //Destructors do not accept arguments
{
}


float EmployeeSalary::getHrlyWage(float HrlyWage)
{
    return HrlyWage;
}

int EmployeeSalary::getHrs(int Hrs)       
{
     return Hrs;
}

float EmployeeSalary::displayPay(float pay)
{                                         
    return Hrs * HrlyWage;                                //calculation to return pay
}                                         


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "salary.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string name;
	int Hrs = 0;
	float HrlyWage = 0.00; 
	float pay = Hrs * HrlyWage;

	EmployeeSalary salary;                     //creating 'salary' object

    cout <<"Please enter your name: ";
    cin >> name;   
    cout << endl;

    cout <<"\nPlease enter your hours worked this week: ";
    cin >> Hrs; 
    salary.getHrs(Hrs);
    cout << endl;

    cout <<"\nPlease enter your hourly rate: ";
    cin>> HrlyWage;
    salary.getHrlyWage(HrlyWage);
    cout <<endl;

    
    cout <<"\n Your paycheck this week will be: $"<<salary.displayPay(pay);
    cout <<endl;

  cin.get();
  cin.get();
  return 0;
}


I'm sure it's obvious to you all but I just can't see it with my very limited C++ troubleshooting experience. Any help would make my day!

Thank you.





You need to calculate pay *after* Hrs and HrlyWage is set (after line 27) and not at line 12.
Thank you PanGalactic,

I repositioned "float pay = Hrs. * HrlyWage" down to line 30 just before the cout stream but it still gives me the same erroneous 'pay' error.

I don't understand why it's not calculating correctly.

Main.cpp
1
2
3
float pay = Hrs * HrlyWage;
cout <<"\n Your paycheck this week will be: $"<<salary.displayPay(pay);
cout <<endl;
The problem is in your "get" functions.

I think what you want is for them to be "Set' functions.

1
2
3
4
float EmployeeSalary::getHrlyWage(float HrlyWage)
{
    return HrlyWage;
}


This code is a really not what you want to do.

You are passing in a variable (the parameter HrlyWage), and then you are returning HrlyWage immediately. Because of variable hiding, which is an issue of scope which you will need to read up on, this will return the parameter you passed in, and will not change the EmployeeSalary instance at all.

Instead what you want to do is this:
1
2
3
4
void EmployeeSalary::setHrlyWage(float newHrlyWage)
{
    HrlyWage = newHrlyWage;
}


This function "sets" the value of HrlyWage in the instance of EmployeeSalary upon which it is called.

Your call in main, then changes to:
 
    salary.setHrs(Hrs);


You will have to make a few more similar changes throughout the code -- hopefully this'll get you on your way though.

Edit --
Oh, and the reason that you are getting a crazy value is because neither of Hrs, HrlyWage are properly instantiated, they are set to some random value. If you initialized the values to zero in your default constructor (the one with no parameters), then you would get it printing 0.00 as your salary value.
Last edited on
Topic archived. No new replies allowed.