-3

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; } 

enter image description here

3
  • 3
    That's because "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. Commented Apr 12, 2020 at 0:22
  • 1
    The getEmployeData member is printing the values of the four members of an otherwise-untouched, and seemingly pointless, object four times. The array list you worked to populate is completely unused once populated. If you want to print the contents of list, then loop iterate that array and print each Employee therein. Commented Apr 12, 2020 at 0:24
  • Does this answer your question? How to Initialize Member Variables Commented Apr 12, 2020 at 2:16

2 Answers 2

0

First you need to modify your getEmployeeData() function like this :

void getEmployeeData() { cout << empNumber<<" "; cout << hours << " ";; cout << payRate << " ";; cout << name << " ";; } 

Then just replace your main code with this one :

int main() { Employee e[4]; e[0] = Employee(9991, 25, 15, "Jeff"); e[1] = Employee(8791, 21, 15, "Mohamed"); e[2] = Employee(9211, 15, 35, "Mary"); e[3] = Employee(5271, 35, 15, "Bob"); for (int i = 0; i < 4; i++) { e[i].getEmployeeData(); cout << endl; } system("Pause"); return 0; } 

This problem with your earlier code was that your were not using the list you created but other instance of the class.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This has helped me get the code to work :D
0

Your constructors are initializing local variables with names that are similar to your members. Actually, if you use g++ to compile, if you put -Wall flag in your compilation, it will warn you about it.

Also, C++ doesn't necessarily initialize the members according to the order of appearance in the constructor (it may be initialized by the order they were declared), therefore, always initialize your members according to the order they were declared!!!. This can cause bugs that are hard to debug.

Your code should be:

#include <string> class Employee { int empNumber; double hours; double payRate; std::string name; public: Employee() : empNumber(0), hours(0), payRate(0), name("Name") { } Employee(int empNmb, double hrs, double payRT, std::string nm) : empNumber(empNmb), hours(hrs), payRate(payRT), name(nm) { } }; 

About the printing - there is no connection between the List<Employee> to the class itself, therefore, you cannot access it from inside of the class. There are two options, either use getters/setters (which is common), or create a print function that print one object, and then loop on the objects in the list.

// inside of class Employee int get_empNumber() const { return empNumber; } double get_hours() const { return hours; } double get_payRate() const { return payRate; } std::string get_name() const { return name; } void print() const { std::cout << "(" << empNumber << ", " << hours << ", " << payRate << ", " << name << std::endl; } 

1 Comment

@OliverVentura added to my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.