Hello!
I'm working on this linked list project where I'm supposed to save the employee data in a linked list and apply some operations.
and I am new with c++ so I'll show you my code and please help me with it.
my questions are:
1- whenever I enter a full name the program keeps going in an infinite loop! i used getline() method and still its not working. but if I enterd just the first name it works fine.
2- I don't know how I can display or find the employee information if I used the methods I create it gives me random data.
my code:
Main:
#include<iostream>
#include<string>
#include <fstream>
#include "EmployeeList.cpp"
using namespace std;
int main(){
string name;
int choice, id;
double Salary;
EmployeeList* employee = new EmployeeList();
cout<<"***********************************************"<<endl;
cout<<"**********EMPLOYEE MANAGEMENT SYSTEM***********"<<endl;
cout<<"***********************************************\n\n"<<endl;
do{
cout<<" 1--->Press 1 to INSERT EMPLOYMENT:"<<endl;
cout<<" 2--->Press 2 to FIND EMPLOYMENT INFO ID:"<<endl;
cout<<" 3--->Press 3 to DELETE EMPLOYMENT ID:"<<endl;
cout<<" 4--->Press 4 to SEARCH HIGHEST SALARY:"<<endl;
cout<<" 5--->Press 5 to DISPLAY ALL EMOLYMENTS:"<<endl;
cout<<" 6--->Press 6 to REVERSE DISPLAY ALL EMPLOYMENTS:"<<endl;
cout<<" 7--->Press 7 to EXIT:"<<endl;
cout<<"\n Enter Your Choice: ";
cin>>choice;
switch(choice){
case 1:
cout<<endl;
cout<<"Enter name : ";
getline(cin,name);
cout<<"Enter ID : ";
cin>>id;
cout<<"Enter Salary : ";
cin>>Salary;
cout<<endl;
employee ->INSERTEMPLOYMENT(name,id,Salary);
break;
case 2:
cout<<endl;
cout<<"Enter ID : ";
cin>>id;
cout<<endl;
employee->FINDEMPLOYMENTINFOID(id);
break;
case 3:
cout<<endl;
cout<<"Employee ID : ";
cin>>id;
cout<<endl;
employee-> DELETEEMPLOYMENTID(id);
break;
case 4:
cout<<endl;
employee->SEARCHHIGHESTSALARY();
break;
case 5:
cout<<endl;
employee->DISPLAYALLEMPLOYMENTS();
break;
case 6:
cout<<endl;
employee->REVERSEDISPLAYALLEMPLOYMENTS(employee->getHead());
break;
case 7:
exit(0);
default:
cout<<"Invalid choice."<<endl;
break;
}
} while (true);
return 0;
}
Employee:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Employee{
private:
string name;
int ID;
double Salary;
Employee* next;
public:
//Constructor
Employee (string n, int id, double s){
n = name;
id = ID;
s = Salary;
this->next = NULL;
}
//Setters
void setName(string n){
n = name;
}
void setID(int id){
id = ID;
}
void setSalary(double s){
s = Salary;
}
void setNext(Employee* next){
this->next = next;
}
//Getters
string getName(){
return name;
}
int getID(){
return ID;
}
double getSalary(){
return Salary;
}
Employee* getNext(){
return this->next;
}
};
EmployeeList:
#include "Employee.cpp"
using namespace std;
class EmployeeList{
private:
Employee* head;
public:
//Constructor
EmployeeList(){
head = NULL;
}
//getter
Employee* getHead(){
return head;
}
//insert method
void INSERTEMPLOYMENT (string name, int id, double Salary){
Employee* newnode = new Employee(name, id, Salary);
if(head == NULL){
head = newnode;
}
else {
Employee* temp = head;
while(temp->getNext() != NULL){
temp = temp->getNext();
}
temp->setNext(newnode);
}
cout<<"The employee record was added successfully."<<endl;
cout<<"------------------------------------------"<<endl;
}
//info
void FINDEMPLOYMENTINFOID (int id){
Employee *temp = head;
bool status = false;
while(temp != NULL){
if(temp->getID() == id){
status = true;
cout<<"Employee Name = "<<temp->getName()<<endl;
cout<<"Employee ID = "<<temp->getID()<<endl;
cout<<"Employee Salary = "<<temp->getSalary()<<endl;
cout<<"---------------------------------------"<<endl;
}
temp = temp->getNext();
}
if(status == false) {
cout<<"Employee is not found in the list."<<endl;
cout<<"--------------------------------------------"<<endl;
}
}
//display
void DISPLAYALLEMPLOYMENTS (){
Employee* temp = head;
while(temp != NULL){
cout<<"Employee Name = "<<temp->getName()<<endl;
cout<<"Employee ID = "<<temp->getID()<<endl;
cout<<"Employee Salary = "<<temp->getSalary()<<endl;
cout<<"---------------------------------------"<<endl;
temp = temp->getNext();
}
}
};