1

I'm just learning C++ and new to programming and I'm having trouble with this problem. I need to assign data to a linked list, the simple data comes in from two separate files. the problem is only the first line of data is being assigned to the linked list, then the list is NULL. I don't understand why its not pulling through. Any ideas would be appreciated.

My sense is that I'm not creating a new Employee correctly and therefore its not assigning it in this part:

while (inFile >> idNum) {

        EmployeeData.addNode(newEmployee);
        inFile >> payRate;
        getline(inFile, fullName);
        newEmployee.setData(fullName, idNum, payRate);

               while (inHoursFile >> hoursID)
        {
            inHoursFile >> newHours;

            if (hoursID == idNum) {

                newEmployee.setHours(newHours); 
                newEmployee.setPayroll();
            }
        }

Here's the full code:

#include <iostream>
#include<cstdlib>
#include <fstream>
#include <string>

using namespace std;

template <class T>
class List;

void openInputFile(ifstream& inFile);

template <class T>
class node {

    T data; // stores data 
    node<T>* next; // stores the next piece of the data 

public:
    node(T newdata = T (), node<T>* newNext = nullptr) : 
        data(newdata), next(newNext){}
    friend class List <T>; 

    //cout this->data 

};

template <class T>
class List {

    node<T>* head;

public: 
    List() : head() {}
    List(const List& rhs) : head(nullptr) { *this = rhs;  } // copy constructor
    List<T>& operator=(const List<T>& rhs); // assignment operator 
    //~List() { clear(); } // destructor
    //void clear(); 
    bool isEmpty() const { return head == nullptr;  }
    void addNode(T newData);
    void insertAtHead(T newData); 
    T* getHead();
    friend int getID();
    friend ostream& operator <<(ostream& outs, T& newEmployee);
    void print();

};

class Employee {

    // constructor 
public:
    Employee() : name(""), idNum(), payRate(), hours(0), payRoll() {}

    //variables 
//private: REMOVE FOR NOW UNTIL YOU FIGURE THIS OUT 
    string name;
    int idNum;
    double payRate;
    int hours;
    double payRoll;

    // functions 
   // int getHours(int idNum, int hours);
    void setPayroll();

    void setData(string newName, int newIDNum, double newPayRate);
    friend istream& operator >>(istream& ins, Employee& newEmployee);
    int getID();
    void setHours(int newHours); 

    template <class T> friend class List;
    template <class T> friend class node;

};

int main()
{
    List<Employee> EmployeeData;
    ifstream inFile;
    ifstream inHoursFile;   

    //Employee newEmployee;
    int idNum;
    double payRate;
    string fullName;
    int hoursID; 
    int newHours;
    Employee newEmployee;

    openInputFile(inFile);
    openInputFile(inHoursFile);

    while (inFile >> idNum) {

        EmployeeData.addNode(newEmployee);
        inFile >> payRate;
        getline(inFile, fullName);
        newEmployee.setData(fullName, idNum, payRate);

               while (inHoursFile >> hoursID)
        {
            inHoursFile >> newHours;

            if (hoursID == idNum) {

                newEmployee.setHours(newHours); 
                newEmployee.setPayroll();
            }
        }



        //CONFIRMS THE DATA IS GOING IN CORRECTLY
               EmployeeData.print();
    }

    return 0;
}

template <class T> 
void List<T>::print() {

    //print function for the node, and then in the linked list class, and then just keep calling print node;; 

    node<T>* temp = head->next;

    while (temp != NULL) {
        //add a getter in here 
        cout << temp->data.name << endl;
        cout << temp->data.payRoll << endl;
        //try call print node 
        temp = temp->next;
    }

    //set for loop;;  node<T>* temp = head; temp != NULL; temp->next;
}

template <class T>
void List<T>::addNode(T newData) {

    if (head == nullptr) {
        insertAtHead(newData);
        return; 
    }

    node<T>* temp = new node<T>(newData); 
    node<T>* end = head; 

    while (end -> next !=nullptr) {
        end = end->next; 
    }

    end->next = temp;

} 

template <class T>
void List<T>::insertAtHead(T newData) {

    head = new node<T>(newData);
    head->next = nullptr; 
}


template <class T>
T* List<T>::getHead() {

    return head;
}


void  Employee::setPayroll() {

    payRoll = payRate * (double)hours;
}

void openInputFile(ifstream& inFile) {

    cout << "Please enter a filename: ";  // The file name is 'employeeinfo.txt' and 'hours' text
    string filename;
    cin >> filename;
    inFile.open(filename);
    while (!inFile) {
        cout << "Bad file name";
        cout << "Please enter a filename: ";
        cin >> filename;
        inFile.clear();
        inFile.open(filename);
    }

}

istream& operator >>(istream& ins, Employee& newEmployee) {

    ins >> newEmployee;

    return ins;
}

void Employee::setData(string newName, int newIDNum, double newPayRate) {

    name = newName;
    payRate = newPayRate;
    idNum = newIDNum;
}

int Employee::getID() {

    return idNum;
}

void Employee::setHours(int newHours) {

    hours += newHours; 
}
Cat C
  • 11
  • 1

0 Answers0