Question

In: Computer Science

Hello, I have created the following code in C++. The goal of this code is to...

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.

Solutions

Expert Solution

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();
};


Related Solutions

Hi, I have created the following code and I was wondering if it is possible to...
Hi, I have created the following code and I was wondering if it is possible to make an "if" statement in the first for loop that would output an error if the user enters a float, character or string? I was thinking of something along the lines of: if(a[i] != int) but that didn't work :( Thank you. #include <iostream> using namespace std; // Creating a constant for the number of integers in the array const int size = 10;...
C++ Hello .I need to convert this code into template and then test the template with...
C++ Hello .I need to convert this code into template and then test the template with dynamic array of strings also if you can help me move the function out of the class that would be great.also There is a bug where the memory was being freed without using new operator. I cant seem to find it thanks in advance #include using namespace std; class DynamicStringArray {    private:        string *dynamicArray;        int size;    public:   ...
Hello I have this error in the code, I do not know how to fix it....
Hello I have this error in the code, I do not know how to fix it. It is written in C++ using a Eclipse IDE Error: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string bus.h =========== #pragma once #include using namespace std; class Bus { private:    string BusId; // bus ID    string Manufacturer; // manufacturer of the bus    int BusCapacity; // bus capacity    int Mileage; // mileage of bus    char Status; // current status...
hello! So I have this CIS assignment lab but when I try to make the code...
hello! So I have this CIS assignment lab but when I try to make the code I don't really know where to start from. My professor is very hard and he likes to see the outcomes as they are shown in the problem. Please help me! Write a program that can be used as a math helper for an elementary student. The program should display two random integer numbers that are to be added, such as:     247 + 129...
hello! So I have this CIS assignment lab but when I try to make the code...
hello! So I have this CIS assignment lab but when I try to make the code I don't really know where to start from. My professor is very hard and he likes to see the outcomes as they are shown in the problem. Please help me! the program should be a C++ PLS Write a program that can be used as a math helper for an elementary student. The program should display two random integer numbers that are to be...
Here is what I have so far. I have created a code where a user can...
Here is what I have so far. I have created a code where a user can enter in their information and when they click submit all of the information is shown. How can I add a required field for the phone number without using an alert? <!Doctype html> <html> <head> <meta charset="UTF-8"> <title>Login and Registeration Form Design</title> <link rel="stylesheet" type="text/css" href="signin.css"> <script> function myFunction(){ document.getElementById('demo').innerHTML = document.getElementById('fname').value + " " + document.getElementById('lname').value + " " + document.getElementById('street').value + " "...
Hello, I have a problem with understanding this code. value.parseHtml().select("p")[0].innerHtml() This code is for the open...
Hello, I have a problem with understanding this code. value.parseHtml().select("p")[0].innerHtml() This code is for the open refine program. Could you tell me what this code is trying to do? I have no idea what this is for... For your information, this code appeared when professor was talking about 'Fetching and Parsing HTML'.. Thank you for answering my question and have a good day! p.s. I don't know whether value.parseHtml().select("p")[0].innerHtml() is a code. Anyways, my professor told us to insert that...
Hello, I Have create this code and Tried to add do while loop but it gives...
Hello, I Have create this code and Tried to add do while loop but it gives me the error in string answar; and the areas where I blod So cloud you please help me to do ( do while ) in this code. // Program objective: requires user to input the data, program runs through the data,calcualtes the quantity, chacks prices for each iteam intered,calautes the prices seperatly for each item, and calculates the amount due without tax, and then...
Hello, this question relates to a class I am taking called introduction to C++. I have...
Hello, this question relates to a class I am taking called introduction to C++. I have no experience writing programs and outside of learning out of a textbook, and studying on my own, have little understanding of logic. I have been assigned a problem that requires me to write a program for a Grocery Bill, where the consumer inputs the price for 5 items, that the program calculates the total with a 6% sales tax. I really am not sure...
Hello, I am working on a C-program that deals with memory mapping, Please show all code...
Hello, I am working on a C-program that deals with memory mapping, Please show all code (code must be in the C) and outputs. The instructions are as follows INSTRUCTIONS: You have two sample c codes, one for the client and one for the server. IF the client makes a change in the shared memory, those changes will show on the server side. Your task is to adapt the change strategy to develop two applications so they can send messages...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT