Question

In: Computer Science

Hi, i'm creating a c++ program with a linked list of Employees, i'm running into issues...

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

}

}

Solutions

Expert Solution

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:


Related Solutions

C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
C++ Write a program to create a linked list which stores the details of employees(Employee number,...
C++ Write a program to create a linked list which stores the details of employees(Employee number, employee name, rate, hours worked). Create a menu to manage the emoployee data. MENU 1. ADD EMPLOYEE DETAILS 2. DELETE EMPLOYEE 3. SEARCH EMPLOYEE 4. PRINT EMPLOYEE PAYROLL 5. EXIT When the user selected option #4 the program should print the following pay report for each employee: EmpNo.     Name      Rate    Hours    Regular Pay      Overtime Pay     Gross Pay Any hours worked above 40 hours are...
In C++ In this lab we will creating two linked list classes: one that is a...
In C++ In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
In C++ please: In this lab we will creating two linked list classes: one that is...
In C++ please: In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list....
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class...
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class to use is probably your first step.(Please do not use collections like .push() . pop(), etc.) and instead create the implementation A templated stack class that stores type T with the following public functions: - void Push(T t) - adds t to the top of the stack - T Pop() - asserts that the stack is not empty then removes and returns the item...
(C++) I'm getting a few errors that prevents the program from running what is causing this?...
(C++) I'm getting a few errors that prevents the program from running what is causing this? " routes3.cpp:202:9: warning: add explicit braces to avoid dangling else [-Wdangling-else] else { ^ " #include <iostream> #include <vector> #include <string> #include <set> #include <cstring> using namespace std; class Leg{ const char* const startCity; const char* const endCity; const int dist; public: Leg(const char* const, const char* const, int); Leg& operator= (const Leg&); int getDist() const {return dist;} void output(ostream&) const; friend class Route;...
Hi I would like to see an example in c++ of Stack As Linked List I...
Hi I would like to see an example in c++ of Stack As Linked List I need a seek() function which receives a double number X and which returns in which position in the stack is X. If the value X is not in the stack, the function should return -1
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
C++ What Should This Program Do? Linked List Class Design your own linked list class (List.h)...
C++ What Should This Program Do? Linked List Class Design your own linked list class (List.h) to hold a series of strings. The linked list node should be implemented as a struct. The class should have member functions for appending, inserting, and deleting nodes. You should also have a display function that will traverse the list & display each node’s value. Don’t forget to add a destructor that destroys the list. DRIVER – driver.cpp Write a driver program (driver.cpp) that...
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse...
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse */ #include <stdio.h> #include <stdlib.h> struct node { int visited; int a; struct node *next; }; int main() { struct node *head = NULL; generate(head); printf("\nPrinting the list in linear order\n"); linear(head); printf("\nPrinting the list in reverse order\n"); display(head); delete(head); return 0; } void display(struct node *head) { struct node *temp = head, *prev = head; while (temp->visited == 0) { while (temp->next !=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT