头文件:
//work.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Work
{
public:
virtual void showInfo() = 0;
virtual string getDeptName() = 0;
int m_Id;
string m_Name;
int m_age;
};
//boss.h
#pragma once
#include<iostream>
#include<string>
#include"work.h"
using namespace std;
class Boss :public Work
{
public:
Boss(int id, string name, int age);
virtual void showInfo();
virtual string getDeptName();
};
//employee.h
#pragma once
#include<iostream>
#include<string>
#include"work.h"
using namespace std;
class Empolyee :public Work
{
public:
Empolyee(int id, string name, int age);
virtual void showInfo();
virtual string getDeptName();
};
//Manager.h
#pragma once
#include<iostream>
#include"work.h"
using namespace std;
class Manager :public Work
{
public:
Manager(int id, string name, int age);
virtual void showInfo();
virtual string getDeptName();
};
//workmanager.h
#pragma once
#include<iostream>
using namespace std;
#include"work.h"
class Workmanager
{
public:
Workmanager();
void Show_Menu();
void Add_Emp();
int m_EmNum;
Work**m_EmpArray;
~Workmanager();
};
源文件:
//boss.cpp
#include"boss.h"
Boss::Boss(int id, string name, int age)
{
m_Id = id;
m_age = age;
m_Name = name;
}
void Boss::showInfo()
{
cout << "职工编号 = " << this->m_Id << endl;
cout << "职工姓名 = " << this->m_Name << endl;
cout << "职工年龄 = " << this->m_age << endl;
cout << "岗位 = " << this->getDeptName()<<endl;
}
string Boss::getDeptName()
{
return string("老板");
}
//***********************
//empolyee.h
#include"empolyee.h"
Empolyee::Empolyee(int id, string name, int age)
{
m_Id = id;
m_age = age;
m_Name = name;
}
void Empolyee::showInfo()
{
cout << "职工编号 = " << this->m_Id << endl;
cout << "职工姓名 = " << this->m_Name << endl;
cout << "职工年龄 = " << this->m_age << endl;
cout << "岗位 = " << this->getDeptName() << endl;
}
string Empolyee::getDeptName()
{
return string("员工");
}
//***********************
//Manager.cpp
#include"Manager.h"
Manager::Manager(int id, string name, int age)
{
m_Id = id;
m_age = age;
m_Name = name;
}
void Manager::showInfo()
{
cout << "职工编号 = " << this->m_Id << endl;
cout << "职工姓名 = " << this->m_Name << endl;
cout << "职工年龄 = " << this->m_age << endl;
cout << "岗位 = " << this->getDeptName() << endl;
}
string Manager::getDeptName()
{
return string("经理");
}
//**************************
//Workmanager.cpp
#include"Workmanager.h"
#include"boss.h"
#include"empolyee.h"
#include"Manager.h"
Workmanager::Workmanager()
{
this->m_EmNum=0;
this->m_EmpArray=nullptr;
};
void Workmanager::Add_Emp()
{
cout << "请输入添加人数" << endl;
int addNum = 0;
cin >> addNum;
if (addNum > 0)
{
int newSize = 0;
newSize = this->m_EmNum + addNum;
//开辟新空间
Work ** newSpace = new Work*[newSize];
if (this->m_EmpArray != nullptr)
{
for (int i = 0; i < this->m_EmNum; i++)
{
newSpace[i] = this->m_EmpArray[i];
}
for (int j = 0; j < addNum; j++)
{
int Id;
string Name;
int age;
cout << "请添加第" << this->m_EmNum + j << "位员工的ID" << endl;
cin >> Id;
cout << "请输入第" << this->m_EmNum + j << "位员工的姓名" << endl;
cin >> Name;
cout << "请输入第" << this->m_EmNum + j << "位员工的年龄" << endl;
cin >> age;
cout << "请选在添加员工岗位" << endl;
cout << "1.boss" << endl;
cout << "2.manager" << endl;
cout << "3.employee" << endl;
int select = 0;
cin >> select;
Work*work = nullptr;
switch (select)
{
case 1:
work = new Boss(Id, Name, age);
break;
case 2:
work = new Manager(Id, Name, age);
break;
case 3:
work = new Empolyee(Id, Name, age);
break;
default:
break;
}
newSpace[this->m_EmNum + j] = work;
}
//删除原有空间
delete[]this->m_EmpArray;
//更改空间指针指向
this->m_EmpArray = newSpace;
//更新职工人数
m_EmNum = newSize;
cout << "成功添加" << addNum << "名员工" << endl;
}
}
else
{
cout << "输入有误,请重新输入" << endl;
}
}
Workmanager::~Workmanager()
{
};
void Workmanager::Show_Menu()
{
cout << "*******************************" << endl;
cout << "***** 欢迎使用职工管理系统*****" << endl;
cout << "******* 0.退出管理程序*********" << endl;
cout << "******* 1.增加职工信息*********" << endl;
cout << "******* 2.显示职工信息*********" << endl;
cout << "******* 3.删除职工信息*********" << endl;
cout << "******* 4.修改职工信息*********" << endl;
cout << "******* 5.查找职工信息*********" << endl;
cout << "******* 6.按照编号排序*********" << endl;
cout << "******* 7.清空所有文档*********" << endl;
cout << "*******************************" << endl;
}
//**********************************
//运行文件
#include<iostream>
#include"workmanager.h"
using namespace std;
int main()
{
Workmanager wm;
wm.Show_Menu();
int select = 0;
while(1)
{
cin >> select;
switch (select)
{
case 1:
wm.Add_Emp();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 0:
{
cout << "欢迎下次使用" << endl;
return 0;
}
}
system("cls");
}
system("pause");
return 0;
}
我现在运行这段代码,在运行下面这串代码的时候,只能运行到cin>>addNum下面的就运行不了,有没有大佬帮我解答一下问题出在哪里了。
void Workmanager::Add_Emp()
{
cout << "请输入添加人数" << endl;
int addNum = 0;
cin >> addNum;
if (addNum > 0)
{
int newSize = 0;
newSize = this->m_EmNum + addNum;
//开辟新空间
Work ** newSpace = new Work*[newSize];
if (this->m_EmpArray != nullptr)
{
for (int i = 0; i < this->m_EmNum; i++)
{
newSpace[i] = this->m_EmpArray[i];
}
for (int j = 0; j < addNum; j++)
{
int Id;
string Name;
int age;
cout << "请添加第" << this->m_EmNum + j << "位员工的ID" << endl;
cin >> Id;
cout << "请输入第" << this->m_EmNum + j << "位员工的姓名" << endl;
cin >> Name;
cout << "请输入第" << this->m_EmNum + j << "位员工的年龄" << endl;
cin >> age;
cout << "请选在添加员工岗位" << endl;
cout << "1.boss" << endl;
cout << "2.manager" << endl;
cout << "3.employee" << endl;
int select = 0;
cin >> select;
Work*work = nullptr;
switch (select)
{
case 1:
work = new Boss(Id, Name, age);
break;
case 2:
work = new Manager(Id, Name, age);
break;
case 3:
work = new Empolyee(Id, Name, age);
break;
default:
break;
}
newSpace[this->m_EmNum + j] = work;
}
//删除原有空间
delete[]this->m_EmpArray;
//更改空间指针指向
this->m_EmpArray = newSpace;
//更新职工人数
m_EmNum = newSize;
cout << "成功添加" << addNum << "名员工" << endl;
}
}
else
{
cout << "输入有误,请重新输入" << endl;
}
}
清空下输入缓存区看看。在cin前面加一个fflush(stdin);
cin那里要你输入一个数字,你有输入吗?