In: Computer Science
Hello,
I have created the following code in C++. The goal of this code is to read 3 types of employee information(String/*Name*/, String/*Phone number*/,Int/*Office number*/, ) from a .txt file and store that information into an array which can be accessed by various functions. The code below is within a header file, and im trying to define some functions (within a .cpp file) of my class to carry out the following tasks.
I have already managed to define a couple functions, but I'm stuck on the two listed below and need help coding them.
Thank you.
CODE:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int SIZE = 25;
struct Phone
{
string employeeName;
string phoneExt;
int officeNumber;
};
class Directory
{
public:
Directory(string/*file name*/, int = SIZE/*max number
of employees*/);
void showPhone(string/*employee phone*/, int
/*employee office*/);
void showPhoneOffice(string/*Name*/) const;
void writeDirectory(ostream &)const;
void addEmployee(string/*Name*/, string/*Phone*/,
int/*Office*/); // this is second function I need help
defining
void showNoEmployees(ostream &) const; //This is
first function I need help defining
int getNoEmployees() const {return noEmployees;}
private:
Phone employees[SIZE];
int maxEmployees;
int noEmployees;
void findEmployee();
};
1. showPhoneOffice() that displays a string(a name) and int( a phone number). The function receives the string and int. It does not return a value. Displays the data or a meaningful error message if the string was not found
2. . addEmployee() that adds an employee(string/*Name*/,string/*Name*/,int/*Office Number*/) to the employee array. The function receives the employee’s string, string and int. It does not return a value. Ensure the employee is not already in an existing array and that the array is not full. You can assume valid data is passed.
Code :
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int SIZE = 25;
struct Phone
{
string employeeName;
string phoneExt;
int officeNumber;
};
class Directory
{
public:
Directory(int SIZE/*max number of employees*/) // no filename is
defined within the class so the string variable is not
required
{
maxEmployees= SIZE;
noEmployees = 0;
}
void showPhone(string phone, int num)
{
if(phone=="NULL")
{
cout<<"The string was not found";
}
else
{
cout<<"Phone number :
"<<phone<<endl<<"Office number :
"<<num<<endl;
}
}
void showPhoneOffice(string name/*Name*/) const
{
if(phone=="NULL")
{
cout<<"The string was not found";
}
else
{
cout<<"Employee Name : "<<name<<endl;
}
}
void writeDirectory(ostream &)const;
void addEmployee(string name, string phone, int office)
{
if(noEmployees >= maxEmployees) // checking if the array has
reached it maximum limit
{
cout<<"The maximum number of employees is reached";
}
else
{
// these statements will assign the given values to the employee
array
employees[noEmployees].employeeName= name;
employees[noEmployees].phoneExt= phone;
employees[noEmployees++].officeNumber= office;
//noEmployees++ will increase the noEmployees by 1 after the
assignment of office is over
}
}
void showNoEmployees(ostream &inputfile) const // id didn't
know whether you wanted just the number of employees or the details
of the employees so i did both
{
// This is if you wanted the number of employees
cout<<"The number of Employees currently available :
"<<noEmployees<<endl;
// If you wanted the details of the employee
string temp1;
int temp2;
while(!inputfile.eof()) // looping with while till the end of
file
{
inputfile >> temp1;
showPhoneOffice(temp1);
inputfile >> temp1 >>temp2;
showPhone(temp1,temp2);
}
}
int getNoEmployees() const
{
return noEmployees;
}
private:
Phone employees[maxEmployees];
int maxEmployees;
int noEmployees;
void findEmployee();
};