Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C++ The following program allows a user to add to and list entries from a

ID: 3583927 • Letter: U

Question

Using C++

The following program allows a user to add to and list entries from a vector, which maintains a list of employees.

Run the program, and provide input to add three employees' names and related data. Then use the list option to display the list.

Modify the program to implement the deleteEntry function.

Run the program again and add, list, delete, and list again various entries.

#include <iostream>

#include <string>

#include <vector>

using namespace std;

// Add an employee

void AddEmployee(vector<string> &name, vector<string> &department,

                 vector<string> &title) {

   string theName = "";

   string theDept = "";

   string theTitle = "";

   cout << endl << "Enter the name to add: " << endl;

   getline(cin, theName);

   cout << "Enter " << theName << "'s department: " << endl;

   getline(cin, theDept);

   cout << "Enter " << theName << "'s title: " << endl;

   getline(cin, theTitle);

   name.push_back(theName);

   department.push_back(theDept);

   title.push_back(theTitle);

   cout << theName << "'s information has been added." << endl;

  

   return;

}

// Delete an employee

void DeleteEmployee(vector<string> &name, vector<string> &department,

                    vector<string> &title) {

   cout << "FIXME: Delete employee" << endl;

   // FIXME: Ask the user for the employee # to delete, then delete that employee

   return;

}

// List all employees

void ListEmployees(vector<string> &name, vector<string> &department,

                   vector<string> &title) {

   int nElements = 0;

   int i = 0;

   nElements = name.size();

   if (nElements > 0) {

      cout << endl;

      for (i = 0; i < nElements; ++i) {

         cout << i << ") Name: " << name.at(i) <<

                      ", Department: " << department.at(i) <<

                      ", Title: " << title.at(i) << endl;

      }

   }

   else {

      cout << endl << "There are no employees to list." << endl;

   }

   return;

}

// Get the action the user wants to perform

char GetAction(string prompt) {

   string answer = "";

   char firstChar = '?';

   cout << endl << prompt << endl;

   getline(cin, answer);

   while(answer.length() == 0) {

      getline(cin, answer);

   }

   firstChar = toupper(answer[0]);

   return firstChar;

}

int main() {

   const char EXIT_CODE = 'X';

   const string PROMPT_ACTION = "Add, Delete, List or eXit (a,d,l,x): ";

   vector<string> name(0);

   vector<string> department(0);

   vector<string> title(0);

   char userAction = '?';

   // Loop until the user enters the exit code.

   userAction = GetAction(PROMPT_ACTION);

   while (userAction != EXIT_CODE) {

      switch(userAction) {

         case 'A': AddEmployee(name, department, title);

                   break;

         case 'D': DeleteEmployee(name, department, title);

                   break;

         case 'L': ListEmployees(name, department, title);

                   break;

         default : // Do nothing

                   break;

      }

      userAction = GetAction(PROMPT_ACTION);

   }

   return 0;

}

Explanation / Answer

#include <iostream>
#include <string>
#include <vector>
using namespace std;

// Add an employee
void AddEmployee(vector<string> &name, vector<string> &department,
vector<string> &title) {
string theName = "";
string theDept = "";
string theTitle = "";

cout << endl << "Enter the name to add: " << endl;
getline(cin, theName);
cout << "Enter " << theName << "'s department: " << endl;
getline(cin, theDept);
cout << "Enter " << theName << "'s title: " << endl;
getline(cin, theTitle);

name.push_back(theName);
department.push_back(theDept);
title.push_back(theTitle);

cout << theName << "'s information has been added." << endl;
  
return;
}

// Delete an employee
void DeleteEmployee(vector<string> &name, vector<string> &department,
vector<string> &title) {
// cout << "FIXME: Delete employee" << endl;
string theName = "";
string theDept = "";
string theTitle = "";

cout << endl << "Enter the name to delete: " << endl;
getline(cin, theName);
cout << "Enter " << theName << "'s department: " << endl;
getline(cin, theDept);
cout << "Enter " << theName << "'s title: " << endl;
getline(cin, theTitle);
// FIXME: Ask the user for the employee # to delete, then delete that employee
// deleting all the matched employee, if more than one emp with same name,dept,title exist
int i=name.size()-1;
int any=0;
for(;i>=0;i--){
if(name[i]==theName && department[i]==theDept && title[i]==theTitle){
name.erase(name.begin()+i);
department.erase(department.begin()+i);
title.erase(title.begin()+i);
any++;
}
}
if(any>0){
cout << theName << "'s information has been deleted." << endl;
}
else{
cout << theName << "'s information has not found." << endl;
}
return;
}

// List all employees
void ListEmployees(vector<string> &name, vector<string> &department,
vector<string> &title) {
int nElements = 0;
int i = 0;

nElements = name.size();
if (nElements > 0) {
cout << endl;
for (i = 0; i < nElements; ++i) {
cout << i << ") Name: " << name.at(i) <<
", Department: " << department.at(i) <<
", Title: " << title.at(i) << endl;
}
}
else {
cout << endl << "There are no employees to list." << endl;
}
return;
}

// Get the action the user wants to perform
char GetAction(string prompt) {
string answer = "";
char firstChar = '?';

cout << endl << prompt << endl;
getline(cin, answer);
while(answer.length() == 0) {
getline(cin, answer);
}
firstChar = toupper(answer[0]);

return firstChar;
}

int main() {
const char EXIT_CODE = 'X';
const string PROMPT_ACTION = "Add, Delete, List or eXit (a,d,l,x): ";

vector<string> name(0);
vector<string> department(0);
vector<string> title(0);

char userAction = '?';

// Loop until the user enters the exit code.
userAction = GetAction(PROMPT_ACTION);

while (userAction != EXIT_CODE) {
switch(userAction) {
case 'A': AddEmployee(name, department, title);
break;
case 'D': DeleteEmployee(name, department, title);
break;
case 'L': ListEmployees(name, department, title);
break;
default : // Do nothing
break;
}

userAction = GetAction(PROMPT_ACTION);
}

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote