In: Computer Science
Hi, i'm creating a c++ program with a linked list of Employees, i'm running into issues as far as displaying programmers only portion and the average salary of all the employees. Please make any changes or comments !
Employee.h file:
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include
using namespace std;
enum Role {programmer, manager, director};
struct Employee
{
std::string firstName;
std::string lastName;
int SSN;
std::string department;
double salary;
Role role;
};
#endif
#ifndef NODE_H
#define NODE_H
Node.h file:
#include "Employee.h"
typedef Employee EMP;
struct Node
{
EMP e;
Node * next;
Node();
Node(EMP);
Node(EMP,Node* next);
};
Node::Node()
{
next=NULL;
}
Node::Node(EMP e_data)
{
e=e_data;
}
Node::Node(EMP e_data, Node* next)
{
e=e_data;
this->next= next;
}
#endif
Main cpp file:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Employee.h"
#include "Node.h"
using namespace std;
void setSalary (Employee& emp)
{
emp.salary= 45000+rand() %20000; // generates salary for employee
}
void setRoles(Employee& emp)
{
emp.role = static_cast(rand()%3); // gives employee job title
}
void setSSN (Employee& emp)
{
// int y = x;
emp.SSN =rand()%499999999+400000000; // sets employee SSN
}
void setFirst( Employee& emp, string* name)
{
int y = rand()%5;
emp.firstName = name[y]; //gives random first name
}
void setLast(Employee& emp, string* name)
{
int y = rand()%5;
emp.lastName = name[y]; // gives random last name
}
void setDepartment(Employee& emp, string * dep)
{
int y = rand()%3;
emp.department= dep[y]; //gives employee random department
}
int main()
{
srand(time(NULL)); // random number generator
double avgSalary; // initialize Average Salary
Node* head= NULL; // starting node
Node* prev= NULL; // node next to the last one
Employee e; // object instance
// array of names, roles(job titles), and departments
string roleType [3] = {"programmer", "manager", "director"};
string firstName[5] = {"John", "James", "Joe", "Jessica", "Juno" };
string lastName[5] = {"Smith", "Williams", "Jackson", "Jones", "Johnson" };
string department[5]= {"Accounting", "IT", "Sales" };
// Create linked list of employees
for (int i = 0; i<10; i++)
{
Employee e;
// function calls (roles, salary ...etc)
setFirst(e, firstName);
setLast(e, lastName);
setSSN(e);
setSalary(e);
setRoles(e);
setDepartment(e, department);
// creates nodes for employees
Node*temp = new Node(e);
if (i ==0)
{
head=temp;
prev= temp;
}
else
{
prev->next= temp;
prev = temp; // links the nodes together
}
}
// Display information of all Employees
cout<<"======== Employee Data======="<
prev = head; // starting at the first node
while(prev !=NULL)
{
cout<<(prev->e).firstName<< " ";
cout<<(prev->e).lastName<< " ";
cout<<(prev->e).SSN<< " ";
cout<<(prev->e).salary<< " ";
cout<<(prev->e).department<< " ";
cout
prev= prev->next;
}
cout<<" \n \n";
//
cout<<"======== Programmer Only Data======="<
prev = head; // starts at beginning of linked list
while(prev !=NULL)
{
if (prev->e.role == 0) // checks to see if role is 0 or programmer
cout<<(prev->e).firstName<< " ";
cout<<(prev->e).lastName<< " ";
cout<<(prev->e).SSN<< " ";
cout<<(prev->e).salary<< " ";
cout<<(prev->e).department<< " ";
cout
prev= prev->next; // moves on to next node
// else {
// break;
// }
}
//Computes Average Salary
prev = head; // starts at the first node
while ( prev !=NULL)
{
avgSalary += (prev->e).salary;
prev = prev->next;
}
// Display Average Salary
cout<< "Average Salary: "<< (avgSalary)/5 << "\n";
// Deallocate memory
Node* temp = head;
Node* tail;
while (temp !=NULL)
{
tail=temp->next;
delete temp;
temp = tail;
}
head=NULL;
if (head == NULL )
{
cout<<" List Deleted \n";
}
}
Here the issue was that the next pointer of the new node which is created was not being set to nullptr because of which it was pointing to garbage location. I have included the comment in the main function where the change is made.
Code:
int main()
{
srand(time(NULL)); // random number generator
double avgSalary = 0.0; // initialize Average Salary
Node* head = NULL; // starting node
Node* prev = NULL; // node next to the last one
Employee e; // object instance
// array of names, roles(job titles), and departments
string roleType[3] = { "programmer", "manager", "director" };
string firstName[5] = { "John", "James", "Joe", "Jessica", "Juno" };
string lastName[5] = { "Smith", "Williams", "Jackson", "Jones", "Johnson" };
string department[5] = { "Accounting", "IT", "Sales" };
// Create linked list of employees
for (int i = 0; i < 10; i++)
{
Employee e;
// function calls (roles, salary ...etc)
setFirst(e, firstName);
setLast(e, lastName);
setSSN(e);
setSalary(e);
setRoles(e);
setDepartment(e, department);
// creates nodes for employees
Node*temp = new Node(e);
//We have to make the next
pointer of the node as nullptr as we are checking
//For null to determine the end of
the list
//If we do not put the next pointer
of the node as nullptr it will have garbage value.
temp->next =
nullptr;
if (i == 0)
{
head = temp;
prev = temp;
}
else
{
prev->next = temp;
prev = temp; // links the nodes together
}
}
// Display information of all Employees
cout << "======== Employee Data=======";
prev = head; // starting at the first node
while (prev != NULL)
{
cout << (prev->e).firstName << " ";
cout << (prev->e).lastName << " ";
cout << (prev->e).SSN << " ";
cout << (prev->e).salary << " ";
cout << (prev->e).department << " ";
prev = prev->next;
cout << " \n \n";
}
//
cout << "======== Programmer Only Data=======";
prev = head; // starts at beginning of linked list
while (prev != NULL)
{
if (prev->e.role == 0) // checks to see if role is 0 or programmer
cout << (prev->e).firstName << " ";
cout << (prev->e).lastName << " ";
cout << (prev->e).SSN << " ";
cout << (prev->e).salary << " ";
cout << (prev->e).department << " ";
prev = prev->next; // moves on to next node
cout << "\n\n";
}
//Computes Average Salary
prev = head; // starts at the first node
while (prev != NULL)
{
avgSalary += (prev->e).salary;
prev = prev->next;
}
// Display Average Salary
cout << "\n\nAverage Salary: " << (avgSalary) / 5 << "\n";
// Deallocate memory
Node* temp = head;
Node* tail;
while (temp != NULL)
{
tail = temp->next;
delete temp;
temp = tail;
}
head = NULL;
if (head == NULL)
{
cout << "\n List Deleted \n";
}
}
OUTPUT: