我正在处理这个链表项目,我应该将员工数据保存在链表中并应用一些操作。我是c++的新手,所以我会给你看我的代码,请帮我。我的问题是:
1-每当我输入全名时,程序就会无限循环!我使用了getline((方法,但仍然不起作用。但如果我只输入第一个名字,效果很好。
2-如果我使用我创建的方法,我不知道如何显示或查找员工信息,它会给我随机数据。我的代码:
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<<"***********************************************nn"<<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;
}
员工:
#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;
}
};
员工列表:
#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();
}
}
};
这里有两个主要问题。
-
赋值是不可逆的。。。
在
Employee
类中,大多数赋值是颠倒的。构造函数是://Constructor Employee (string n, int id, double s){ n = name; // replace the local copy of n with name!!! id = ID; s = Salary; this->next = NULL; }
它当然应该是:
//Constructor Employee(string n, int id, double s) { name = n; ID = id; Salary= s; this->next = NULL; }
这就是为什么
Employee
实例只包含垃圾值的原因。。。 -
混合
getline
和流提取器是危险的当您使用流提取器读取整数值时,流指针会直接位于最后一个数字之后,因此就在行尾之前。如果在那之后立即使用
getline
,您将只读取换行符,并将获得一个空字符串。一个简单的技巧是始终跳过空行:case 1: cout << endl; cout << "Enter name : "; for (;;) { getline(cin, name); if (name != "") break; } ...
这应该足以解决眼前的问题。但其他人仍然存在:
-
您永远不会测试输入流的状态:如果用户键入了不正确的数字,您将不会检测到它,并且您的程序将具有错误的行为
-
作为链表的
EmployeeList
应该表现为一个容器:它应该自动销毁其析构函数中的元素。由于这一点和5的规则,它还应该有一个自定义(或已删除(的复制构造函数和复制赋值运算符。 -
构造函数有一种特殊的语法,可以直接初始化它们的成员,而不是先默认初始化它们,然后分配给它们。它不仅效率稍高,而且是初始化const成员的唯一方法。因此,惯用的方法应该是:
//Constructor Employee (string n, int id, double s) : name(n), ID(id), Salary(s), next(nullptr) {}