In: Computer Science
I am having trouble fixing my build errors. The compiler I am using is Visual Studio.Thanks.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
/*
* structure to store employee details
* employee details are stored as linked list
*/
struct Employee
{
private:
string full_name; //full name
double net_income; //income
public:
struct Employee *next; //pointing to the next employee
details in the list
/*
* create
new employee node
*/
Employee(string full_name, double net_income)
{
this->full_name =
full_name;
this->net_income =
net_income;
this->next = NULL;
}
/*
* display the details of this employee
*/
void displayDetails()
{
cout << "\tFull name : "
<< full_name << endl;
cout << "\tDaily salary: "
<< net_income << endl << endl;
}
};
/*
* Implementation of Queue
*/
class EmployeeQueue
{
struct Employee *front = NULL, *rear = NULL; // front
and rear pointers of queue
public:
/*
* inset a new employee details into queue
* this function take argument as a pointer to employee
struct
*/
void enqueue(struct Employee *newEmployee)
{
if (rear == NULL)
{
/*
* empty
wueue
*/
rear =
newEmployee;
front =
newEmployee;
}
else
{
/*
* not
empty
* inseting new
details at rear
*/
rear->next =
newEmployee;
rear =
newEmployee;
}
}
/*
* remove employee who is in front of the queue
* this function return a pointer to employee
struct
*/
Employee* dequeue()
{
if (front == NULL)
{
/*
* empty
queue
*/
return
NULL;
}
/*
* details of employee who is in
front
*/
struct Employee *leavingEmployee =
front;
front = front->next;
if (front == NULL)
rear = NULL;
return leavingEmployee;
}
};
int main()
{
Employee * employee; //pointer of employee
structure
EmployeeQueue *queue = new EmployeeQueue(); //pointer
of employee queue
int ch = 0;
string first_name;
string last_name;
string full_name;
double work_hours;
double hourly_rate;
double tax_rate;
double gross_salary;
double net_salary;
/*
* iterate user inputs until exit command
*/
while (ch != 3)
{
/*
* displaying main menu
*/
cout << endl << "1.
Enter an employee information" << endl;
cout << "2. Display
information of employee who is leaving the work" <<
endl;
cout << "3. Exit program"
<< endl;
cout << "Enter your choice:
";
cin >> ch; //getting
choice
/*
* processing user choice
*/
}
switch (ch)
{
case 1:
{ /*
* choice to add new employee
*
* getting input of employee
details
*/
cout << "\tEnter first name
of employee : ";
cin >> first_name;
cout << "\tEnter the last
name of the employee : ";
cin >> last_name;
cout << "\tEnter working
hours of the employee : ";
cin >> work_hours;
cout << "\tEnter the hourly
working rate of the employee: ";
cin >> hourly_rate;
cout << "\tEnter the tax rate
(%) of the employee : ";
cin >> tax_rate;
/*
* finding full name
*
* full name = first name + last
name
*/
full_name = first_name + " " +
last_name;
/*
* calculating gross salary
*
* gross salary = working hours *
hourly rate
*/
gross_salary = (work_hours *
hourly_rate);
/*
* calculating net salary
*
* net salary = gross salary -
(gross salary * (tax rate / 100)
*/
net_salary = gross_salary -
(gross_salary * tax_rate / 100.0);
/*
* creating new employee
structure
*/
employee = new Employee(full_name,
net_salary);
/*
* inserting new employee details
into queue
*/
queue->enqueue(employee);
}
break;
{case 2:
/*
* choice to display details
*/
/*
* getting details of employee who
is leaving
*/
employee =
queue->dequeue();
}
if (employee != NULL)
{
/*
* if not null displaying
details
*/
cout << "\tDetails of
employee who is leaving:" << endl;
employee->displayDetails();
}
else
{
/*
* queue is empty
*/
cout << "Queue is empty."
<< endl;
}
break;
{ case 3:
/*
* choice to exit
*
* free all memories allocated
dynamically
*/
Employee * employee = NULL;
free(employee);
free(queue);
break;
default:
/*
* invalid choice
*/
cout << "Invalid choice.
Please enter it again." << endl;
}
}
system("pause");
return 0;
};
//corrected program
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
/*
* structure to store employee details
* employee details are stored as linked list
*/
struct Employee
{
private:
string full_name; //full name
double net_income; //income
public:
struct Employee *next; //pointing to the next employee details in
the list
/*
* create new employee node
*/
Employee(string full_name, double net_income)
{
this->full_name = full_name;
this->net_income = net_income;
this->next = NULL;
}
/*
* display the details of this employee
*/
void displayDetails()
{
cout << "\tFull name : " << full_name <<
endl;
cout << "\tDaily salary: " << net_income << endl
<< endl;
}
};
/*
* Implementation of Queue
*/
class EmployeeQueue
{
struct Employee *front = NULL, *rear = NULL; // front and rear
pointers of queue
public:
/*
* inset a new employee details into queue
* this function take argument as a pointer to employee struct
*/
void enqueue(struct Employee *newEmployee)
{
if (rear == NULL)
{
/*
* empty wueue
*/
rear = newEmployee;
front = newEmployee;
}
else
{
/*
* not empty
* inseting new details at rear
*/
rear->next = newEmployee;
rear = newEmployee;
}
}
/*
* remove employee who is in front of the queue
* this function return a pointer to employee struct
*/
Employee* dequeue()
{
if (front == NULL)
{
/*
* empty queue
*/
return NULL;
}
/*
* details of employee who is in front
*/
struct Employee *leavingEmployee = front;
front = front->next;
if (front == NULL)
rear = NULL;
return leavingEmployee;
}
};
int main()
{
Employee * employee; //pointer of employee structure
EmployeeQueue *queue = new EmployeeQueue(); //pointer of employee
queue
int ch = 0;
string first_name;
string last_name;
string full_name;
double work_hours;
double hourly_rate;
double tax_rate;
double gross_salary;
double net_salary;
/*
* iterate user inputs until exit command
*/
while (ch != 3)
{
/*
* displaying main menu
*/
cout << endl << "1. Enter an employee information"
<< endl;
cout << "2. Display information of employee who is leaving
the work" << endl;
cout << "3. Exit program" << endl;
cout << "Enter your choice: ";
cin >> ch; //getting choice
/*
* processing user choice
*/
switch (ch)
{
case 1:
{ /*
* choice to add new employee
*
* getting input of employee details
*/
cout << "\tEnter first name of employee : ";
cin >> first_name;
cout << "\tEnter the last name of the employee : ";
cin >> last_name;
cout << "\tEnter working hours of the employee : ";
cin >> work_hours;
cout << "\tEnter the hourly working rate of the employee:
";
cin >> hourly_rate;
cout << "\tEnter the tax rate (%) of the employee : ";
cin >> tax_rate;
/*
* finding full name
*
* full name = first name + last name
*/
full_name = first_name + " " + last_name;
/*
* calculating gross salary
*
* gross salary = working hours * hourly rate
*/
gross_salary = (work_hours * hourly_rate);
/*
* calculating net salary
*
* net salary = gross salary - (gross salary * (tax rate /
100)
*/
net_salary = gross_salary - (gross_salary * tax_rate / 100.0);
/*
* creating new employee structure
*/
employee = new Employee(full_name, net_salary);
/*
* inserting new employee details into queue
*/
queue->enqueue(employee);
break;}
case 2:{
/*
* choice to display details
*/
/*
* getting details of employee who is leaving
*/
employee = queue->dequeue();
if (employee != NULL)
{
/*
* if not null displaying details
*/
cout << "\tDetails of employee who is leaving:" <<
endl;
employee->displayDetails();
}
else
{
/*
* queue is empty
*/
cout << "Queue is empty." << endl;
}
break;}
case 3:{
/*
* choice to exit
*
* free all memories allocated dynamically
*/
Employee * employee = NULL;
free(employee);
free(queue);
break;}
default:
/*
* invalid choice
*/
cout << "Invalid choice. Please enter it again." <<
endl;
}
}
system("pause");
return 0;
};
//sample output