Hello, I'm learning how to code in C++ and am still getting used to using functions outside of int main(). Please help
Sample Run
Employee Management System
Command Menu
list - Display all employees
view - View an employee
add - Add an employee
del - Delete an employee
exit - Exit program
Command: list
1. John Rink, [email protected], 415-123-4567
2. Mike Anderson, [email protected],
415-111-2222
Command: view
Number (1 - 2): 3
Invalid number. Try again!
Command: view
Number (1 - 2): 2
Name: Mike Anderson, [email protected],
415-111-2222
Command: add
Name: Andy Patel
Email: [email protected]
Phone: 415-222-3333
Andy Patel was added.
Command: del
Number (1 - 3): 5
This employee is not found. Try again!
Command: del
Number (1 - 3): 1
John Rink was deleted.
Command: list
1. Mike Anderson, [email protected],
415-111-2222
2. Andy Patel, [email protected], 415-222-3333
Command: exit
Thank you for using my app!
Specifications
Screenshot of program code:-
Screenshot of output:-
Program code to copy:-
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
// Define a structure to store the data for each employee
struct Employee
{
string name;
string email;
string phone;
};
//Functions prototype
void display_title();
void display_menu();
void read_file(vector<Employee> &employees);
void display(vector<Employee> &employees);
void view(int num, const vector<Employee>
&employees);
void add(Employee employee, vector<Employee>
&employees);
void del(int num, vector<Employee> &employees);
int main()
{
// Decalre vector of Employee
vector <Employee> employees;
// Declare structure type variable
Employee employee;
// Calling function to display title
display_title();
// Calling function to display menu
display_menu();
// Calling function to read employee data from
file
read_file(employees);
string command;
int num, size;
do
{
// Prompt and read command from
user
cout << "Command: ";
cin >> command;
if(command == "list")
// Calling
function to display all employee records
display(employees);
else if(command == "view")
{
// get the vector size
size = employees.size();
if(size == 0)
cout << "No employee
record exist in the list" << endl;
else
// prompt & read employee
record number to be read from vector
cout << "Number (1 - "
<< size << "): ";
cin >> num;
if(num>size)
cout
<< "Invalid number. Try again!" << endl;
else
// Calling
function to view the record
view(num,
employees);
}
else if(command == "add")
{
cin.ignore();
//Prompt & read employee data from
user
cout << "Name: ";
getline(cin,employee.name);
cout << "Email: ";
getline(cin,employee.email);
cout << "Phone: ";
getline(cin,employee.phone);
// calling function to add employee record into
vector
add(employee, employees);
}
else
if(command == "del")
{
// get the
vector size
size =
employees.size();
if(size == 0)
cout << "No employee
record exist in the list" << endl;
else
// prompt & read employee
record number to be deleted from vector
cout << "Number (1 - "
<< size << "): ";
cin >> num;
if(num>size)
cout
<< "This employee is not found. Try again!" <<
endl;
else
// calling
function to delete employee record from vector
del(num,
employees);
}
}while(command!="exit");
cout << "Thank you for using my app!" <<
endl;
return 0;
}
// Function to display title
void display_title()
{
cout << "Employee Management System" <<
endl;
}
// Function to display menu
void display_menu()
{
cout << "Command Menu" << endl;
cout << "list - Display all employees" <<
endl;
cout << "view - View an employee" <<
endl;
cout << "add - Add an employee" <<
endl;
cout << "del - Delete an employee" <<
endl;
cout << "exit - Exit program" <<
endl;
}
// Function to read read employee record from file and
// store it into vector
void read_file(vector<Employee> &employees)
{
ifstream fin;
fin.open("employees.txt");
Employee emp;
while(!fin.eof())
{
getline(fin,emp.name,'\t');
getline(fin,emp.email,'\t');
getline(fin,emp.phone,'\n');
employees.push_back(emp);
}
}
// Function to display records of all employees in vector
void display(vector<Employee> &employees)
{
//get the number of elements in vector
int n = employees.size();
struct Employee employee;
for(int i=0; i<n; i++)
{
employee = employees[i];
cout << employee.name
<< ", "
<<
employee.email << ", "
<<
employee.phone
<<
endl;
}
}
// Function to read a specific employee record from vector
void view(int num, const vector<Employee>
&employees)
{
struct Employee employee;
employee = employees[num-1];
cout << employee.name << ", "
<< employee.email << ",
"
<< employee.phone
<< endl;
}
// Function to add new employee record into vector
void add(Employee employee, vector<Employee>
&employees)
{
employees.push_back(employee);
cout << employee.name << " was added."
<< endl;
}
// Function to delete specific employee record from file
void del(int num, vector<Employee> &employees)
{
Employee employee = employees[num-1];
employees.erase(employees.begin()+num-1);
cout << employee.name << " was deleted."
<< endl;
}
Before executing above program you need to create a text file named "employees.txt" and store it into your current working folder. The data in text file is seperated by tab space.
Get Answers For Free
Most questions answered within 1 hours.