I am trying to pass 4 different values to each element of my fixed array. However, when I try to output the console outputs memory locations rather than the values I want to see. The aim of the program is to use an array to store Employee data and using a for loop cycle through the data and print it to the console. What must I add in order to convert the array's output from memory location to the desired values?
#include <iostream> #include <cmath> #include <string> using namespace std; class Employee { int empNumber; string name; double hours, payRate; public: Employee() { int empNumber = 0; double hours=0, payRate = 0; string name = "Name"; } Employee(int empNmb, double hrs, double payRT, string nm) { empNumber = empNmb; hours = hrs; payRate = payRT; name = nm; } void getEmployeeData() { for (int i = 0; i < 4; i++) { cout << empNumber; cout << hours; cout << payRate; cout << name; } } }; int main() { Employee e; Employee list[4]; list[0] = Employee(9991, 25, 15, "Jeff"); list[1] = Employee(8791, 21, 15, "Mohamed"); list[2] = Employee(9211, 15, 35, "Mary"); list[3] = Employee(5271, 35, 15, "Bob"); e.getEmployeeData(); system("Pause"); return 0; }
int empNumber = 0;" does not do what you think it does. Neither does "double hours=0, payRate = 0;", nor "string name = "Name";". That's not how you initialize class members in a constructor. All those statements do is create local variables whose names happen to be the same as the class members. But does not really initialize the actual class members. For more information and many examples of initialization class members in your constructor, you should look in your C++ book.getEmployeDatamember is printing the values of the four members of an otherwise-untouched, and seemingly pointless, object four times. The arraylistyou worked to populate is completely unused once populated. If you want to print the contents oflist, then loop iterate that array and print each Employee therein.