online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS Code, Compile, Run and Debug online from anywhere in world. **************************************************************************** SRISHTI BTBTC19163 1913140 *******************************************************************************/ #include <stdio.h> #include <fstream> #include "employeeMgr.h" int main() { printf("Welcome to the Employee Management Program (EMP)\n"); printf("Please read employee data from \"employee_data.txt\" file or add employee details manually\n"); printf("EMP Info: First employee is always Senior Most Employee in the company\n"); printf("EMP Info: Senior Most Employee has Manager Name empty\n"); printf("EMP Info: All other employees must have manager name which is an existing employee to maintain employment hierarchy\n"); employeeMgr mgr; mgr.showMainMenu(); return 0; }
#include <stdio.h> #include <iostream> #include <string> #include <vector> using namespace std; class employee { private: string _firstName; string _lastName; int _empId; float _salary; employee* _mgr; std::vector<employee*> _directReportee; public: employee(string firstName, string lastName, int empId, employee* mgr, float _salary); ~employee(); string getEmployeeData(); string getManagerName(); string getName(); employee* getManager(); float getSalary() {return _salary;} void setSalary(float v) {_salary = v;} void setManager(employee* ptr) {_mgr = ptr;} int getEmpId() {return _empId;} void addToDirectReportingList(employee* ptr); void deleteFromDirectReportingList(employee* ptr); string showAllDirectReportee(); std::vector<employee*> & getDirectReporteeList(); void printEmployeeData(); void setFirstName(string name) {_firstName = name;} void setLastName(string name) {_lastName = name;} };
#include "employee.h" employee::employee(string firstName, string lastName, int empId, employee* mgr, float salary) { _firstName = firstName; _lastName = lastName; _empId = empId; _salary = salary; _mgr = mgr; } employee::~employee() { _directReportee.clear(); } string employee::getEmployeeData() { std::string data; data.append("Name: " + getName()); data.append(" empId: " + std::to_string(_empId)); data.append(" Manager Name: " + getManagerName()); return data; } employee* employee::getManager() { return _mgr; } string employee::getManagerName() { string manager; if (_mgr != nullptr) { manager = _mgr->getName(); } else { manager = "NULL"; } //printf("Manager Name = %s\n", manager.c_str()); return manager; } string employee::getName() { string name; name.append(_firstName + " "+ _lastName); //printf("Employee Name = %s\n", name.c_str()); return name; } // function to add a employee in a manager's addToDirectReportingList void employee::addToDirectReportingList(employee* ptr) { _directReportee.push_back(ptr); } // function to delete a employee from a manager's deleteFromDirectReportingList void employee::deleteFromDirectReportingList(employee* ptr) { std::vector<employee*> tempVec; for(unsigned int i = 0 ; i < _directReportee.size(); i++) { if (_directReportee[i] != ptr) { tempVec.push_back(_directReportee[i]); } } _directReportee.clear(); for(unsigned int i = 0 ; i < tempVec.size(); i++) { _directReportee.push_back(tempVec[i]); } } // show all the reportees of a manager string employee::showAllDirectReportee() { string names; for(unsigned int i = 0; i < _directReportee.size(); i++) { names.append(_directReportee[i]->getName()); names.append(" "); } //printf("Direct Reportee List: %s\n", names.c_str()); return names; } std::vector<employee*>& employee::getDirectReporteeList() { return _directReportee; } // function to display employee;s void employee::printEmployeeData() { printf("Employee data:\n"); printf("Name: %s\n", getName().c_str()); printf("Salary: %f\n", getSalary()); printf("Manager name: %s\n", getManagerName().c_str()); printf("Direct Reportee: %s\n", showAllDirectReportee().c_str()); printf("------------------------------------------------------\n"); }
#include <stdio.h> #include <iostream> #include <string> #include <vector> #include <map> #include "employee.h" #include <fstream> #include <sstream> using namespace std; //class employee; class employeeMgr { private: std::map<string, std::vector<employee*> > _nameToEmpMap; int _empIdx; int _totalEmployees; bool choiceSelector(unsigned maxRange, unsigned &selection); string keyGenerator(string firstName, string lastName); string getFirstName(); string getLastName(); employee* addEmployeeHelper(string firstName, string lastName, employee* mgr, float salary); void deleteEmployeeHelper(string firstName, string lastName, employee* ptr); employee* searchEmployeeHelper(string firstName, string lastName); void showEmployeeHierarchyHelper(string firstName, string lastName, employee* ptr); bool getEmployeeDetails(string &firstName, string &lastName, float &salary, employee* &mgr); bool readEmployeeDetailsFromFileHelper(std::ifstream &fp); bool updateEmployeeDetailsHelper(employee* ptr); public: employeeMgr(); ~employeeMgr(); void addEmployee(unsigned choice); void searchEmployee (); void showAllEmployeesData(); void deleteEmployee(); void showEmployeeHierarchy(); void showMainMenu(); void readEmployeeDetailsFromFile(); void updateEmployeeDetails(); };
#include "employeeMgr.h" employeeMgr::employeeMgr() { _nameToEmpMap.clear(); _empIdx = 0; _totalEmployees = 0; } employeeMgr::~employeeMgr() { _nameToEmpMap.clear(); _empIdx = 0; _totalEmployees = 0; } // helper function to assit in main searchEmployee function employee* employeeMgr::searchEmployeeHelper(string firstName, string lastName) { string key = keyGenerator(firstName, lastName); std::map<string, std::vector<employee*> >::iterator it = _nameToEmpMap.find(key); std::vector<employee*> empVec; if (it != _nameToEmpMap.end()) { empVec = it->second; unsigned choice = 0; if (empVec.size() == 1) { //printf("Found employee with record: %s\n", empVec[choice]->getEmployeeData().c_str()); return empVec[choice]; } printf("Multiple employees found with same name\n"); for (unsigned int i= 0; i < empVec.size(); i++) { employee* emp = empVec[i]; printf("Employee Data: %s To select this Employee press %u\n", emp->getEmployeeData().c_str(), i); } if (choiceSelector(empVec.size(), choice)) { return empVec[choice]; } else { return NULL; } } else { //printf("Employee not found in our database1\n"); return NULL; } } // function to add employee details and check whether the details are valid or not bool employeeMgr::getEmployeeDetails(string &firstName, string &lastName, float &salary, employee* &mgr) { string mgrFirstName, mgrLastName; printf("Enter employee first name\n"); cin >> firstName; printf("Enter employee last name\n"); cin >> lastName; printf ("Enter employee salary\n"); cin >> salary; mgr = NULL; if (_totalEmployees > 0) { printf ("Enter employee manager's first name\n"); cin >> mgrFirstName; printf ("Enter employee manager's last name\n"); cin >> mgrLastName; mgr = searchEmployeeHelper(mgrFirstName, mgrLastName); if (mgr == NULL) { printf("Manager not an existing employee.\n"); return false; } } return true; } // helper function to assit in main addEmployee function employee* employeeMgr::addEmployeeHelper(string firstName, string lastName, employee* mgr, float salary) { _empIdx++; _totalEmployees++; employee* ptr = new employee(firstName, lastName, _empIdx, mgr, salary); //ptr->getName(); //ptr->getManagerName(); string key = keyGenerator(firstName, lastName); std::vector<employee*> empVec; std::map<string, std::vector<employee*> >::iterator it = _nameToEmpMap.find(key); if (it != _nameToEmpMap.end()) { empVec = it->second; } empVec.push_back(ptr); _nameToEmpMap[key] = empVec; printf("Employee %s added successfully\n", key.c_str()); return ptr; } // main function to add new employees in the map void employeeMgr::addEmployee( unsigned choice) { string firstName, lastName; float salary; employee* mgr= NULL; if (choice == 0) { // employees read from file readEmployeeDetailsFromFile(); } else { bool add = getEmployeeDetails(firstName, lastName, salary, mgr); if (!add) { printf("Skiiping addition of employee %s %s as its manager is not defined\n", firstName.c_str(), lastName.c_str()); } else { employee* ptr = addEmployeeHelper(firstName, lastName, mgr, salary); if (mgr != NULL) { mgr->addToDirectReportingList(ptr); } } } showMainMenu(); // return to main menu } // helper function to assit in main readEmployeeDetailsFromFile function bool employeeMgr::readEmployeeDetailsFromFileHelper(std::ifstream &fp) { string line; string firstName, lastName, mgrFirstName, mgrLastName; float salary; employee* mgr; string comment = "#"; string infoLine = "Employee First"; while(std::getline(fp, line)) { if (line.find(comment) != std::string::npos) { // this is a comment line skip it continue; } if (line.find(infoLine) != std::string::npos) { // this is a information line line skip it continue; } firstName.clear(); mgrFirstName.clear(); //printf("Line = %s\n", line.c_str()); std::istringstream iss(line); iss >> firstName; if (firstName.empty()) { continue; } iss >> lastName; iss >> salary; iss >> mgrFirstName; iss >> mgrLastName; if ((mgrFirstName.empty()) || (mgrFirstName == "NULL")) { if (_totalEmployees == 0) { employee* ptr = addEmployeeHelper(firstName, lastName, NULL, salary); } else { printf("Skiiping addition of employee %s %s as its manager is not defined\n", firstName.c_str(), lastName.c_str()); } } else { mgr = searchEmployeeHelper(mgrFirstName, mgrLastName); if (mgr != NULL) { employee* ptr = addEmployeeHelper(firstName, lastName, mgr, salary); mgr->addToDirectReportingList(ptr); //printf("%s %s %f %s %s\n", firstName.c_str(), lastName.c_str(), salary, mgrFirstName.c_str(), mgrLastName.c_str()); } else { printf("Skiiping addition of employee %s %s as its manager doesn't exists in the database\n", firstName.c_str(), lastName.c_str()); } } } fp.close(); return true; } // main function to add employee by using file employee_data void employeeMgr::readEmployeeDetailsFromFile() { std::ifstream infile ("employee_data.txt"); if (infile.is_open()) { readEmployeeDetailsFromFileHelper(infile); } return; } //helper function to assit main deleteEmployee function void employeeMgr::deleteEmployeeHelper(string firstName, string lastName, employee* ptr) { string key = keyGenerator(firstName, lastName); std::map<string, std::vector<employee*> >::iterator it = _nameToEmpMap.find(key); std::vector<employee*> empVec; empVec = it->second; employee* mgr = ptr->getManager(); if (mgr == NULL) { printf("Cannot delete the senior most employee. Deletion cancelled\n"); return; } std::vector<employee*> newEmpVec; for (unsigned int i= 0; i < empVec.size(); i++) { if (empVec[i] != ptr) { newEmpVec.push_back(empVec[i]); } } mgr->deleteFromDirectReportingList(ptr); // copy the direct reportee list of the employee to its manager direct reportee list before Deletion std::vector<employee*> directReportee = ptr->getDirectReporteeList(); for(unsigned int i = 0; i < directReportee.size(); i++) { employee* tmp = directReportee[i]; tmp->setManager(mgr); mgr->addToDirectReportingList(tmp); } // erasing the old vector _nameToEmpMap.erase(it); _totalEmployees--; // adding the new employee vector if (newEmpVec.size() > 0) { _nameToEmpMap[key] = newEmpVec; } delete ptr; printf("Employee deleted successfully\n"); } //main function to delete employee from map void employeeMgr::deleteEmployee() { string firstName = getFirstName(); string lastName = getLastName(); employee* ptr = searchEmployeeHelper(firstName, lastName); if (ptr != NULL) { deleteEmployeeHelper(firstName, lastName, ptr); } else { printf("Employee not found in database. Deletion of employee unsuccessful\n"); } // return to main menu showMainMenu(); } // function to display the employee data void employeeMgr::showAllEmployeesData() { printf("Showing all employees data\n"); std::map<string, std::vector<employee*> >::iterator it = _nameToEmpMap.begin(); for (; it != _nameToEmpMap.end(); ++it) { std::vector<employee*> empVec = it->second; //printf("Key = %s, size = %lu\n", (it->first).c_str(), empVec.size()); for (unsigned int i= 0; i < empVec.size(); i++) { employee* emp = empVec[i]; emp->printEmployeeData(); /* printf("Employee data:\n"); printf("Name: %s\n", emp->getName().c_str()); printf("Salary: %f\n", emp->getSalary()); printf("Manager name: %s\n", emp->getManagerName().c_str()); printf("Direct Reportee: %s\n", emp->showAllDirectReportee().c_str()); printf("------------------------------------------------------\n"); */ //printf("Employee Data: %25s\n", emp->getEmployeeData().c_str()); } } showMainMenu(); } // main function to Search an employee in map void employeeMgr::searchEmployee() { string firstName = getFirstName(); string lastName = getLastName(); employee* ptr = searchEmployeeHelper(firstName, lastName); if (ptr != NULL) { printf("Employee found in the database. Printing employee details:\n"); ptr->printEmployeeData(); //printf ("Employee data: %s\n", ptr->getEmployeeData().c_str()); } else { printf("Employee not found in the database\n"); } // return to main menu showMainMenu(); } // function to give the ranges for the showMainMenu choices bool employeeMgr::choiceSelector(unsigned maxRange, unsigned &selection) { printf("Please choose from range %u - %u\n", 0, maxRange-1); cin >> selection; if (selection < maxRange) { return true; } printf("Selection not in range. Please choose from range %u - %u\n", 0, maxRange-1); cin >> selection; if (selection < maxRange) { return true; } else { printf("Selection not in range. Process killed\n"); selection = maxRange-1; return false; } } // function to generate a key consisting of employee name which is used to search employee by using function searchEmployee string employeeMgr::keyGenerator(string firstName, string lastName) { string key; key.append(firstName+" " +lastName); return key; } // helper function to assit in main showEmployeeHierarchy function void employeeMgr::showEmployeeHierarchyHelper(string firstName, string lastName, employee* ptr) { std::vector<employee*> tmpVec; std::vector<employee*> directReportee = ptr->getDirectReporteeList(); bool hasDirectReportee = (directReportee.size() > 0) ? true : false; tmpVec.push_back(ptr); employee* mgr = ptr->getManager(); while (mgr != NULL) { tmpVec.push_back(mgr); mgr = mgr->getManager(); } string connector = "|"; for (int i = tmpVec.size()-1; i >= 0; i--) { employee* tmp = tmpVec[i]; printf("%30s\n", tmp->getName().c_str()); if (i > 0) { printf("%24s\n", connector.c_str()); } } if (hasDirectReportee) { printf("%24s\n", connector.c_str()); printf("%30s\n", ptr->showAllDirectReportee().c_str()); } } // main function to show Hierarchy in Employee Data Management system void employeeMgr::showEmployeeHierarchy() { string firstName = getFirstName(); string lastName = getLastName(); employee* ptr = searchEmployeeHelper(firstName, lastName); if (ptr != NULL) { showEmployeeHierarchyHelper(firstName, lastName, ptr); } else { printf("Employee not found in database.\n"); } // return to main menu showMainMenu(); } // function to get first name of employee string employeeMgr::getFirstName() { string name; printf("Please enter employee first name\n"); cin >> name; return name; } //function to get last name of employee string employeeMgr::getLastName() { string name; printf("Please enter employee last name\n"); cin >> name; return name; } // helper function to assit main updateEmployeeDetails function /* bool employeeMgr::updateEmployeeDetailsHelper(employee *ptr) { printf("To update name of employee Press 0\n"); printf("To update salary of employee Press 1\n"); unsigned maxChoice = 2, choice; string newFirstName, newLastName; float newSalary; if (ptr->getManager() != NULL) { printf("To update manager of employee Press 2\n"); maxChoice = 3; } if (choiceSelector(maxChoice, choice)) { if (choice == 0) { string oldName = ptr->getName(); newFirstName = getFirstName(); newLastName = getLastName(); //string oldName = ptr->getName(); ptr->setFirstName(newFirstName); ptr->setLastName(newLastName); string key = keyGenerator(newFirstName, newLastName); std::map<string, std::vector<employee*> >::iterator it = _nameToEmpMap.find(key); std::vector<employee*> empVec; empVec = it->second; std::vector<employee*> newEmpVec; for (unsigned int i= 0; i < empVec.size(); i++) { newEmpVec.push_back(empVec[i]); } // erasing the old vector _nameToEmpMap.erase(it); // adding the new employee vector if (newEmpVec.size() > 0) { _nameToEmpMap[key] = newEmpVec; } printf("Updated name of employee from Old name = %s to New Name = %s\n", oldName.c_str(), ptr->getName().c_str()); } else if (choice == 1) { printf("Enter new salary of employee\n"); cin >> newSalary; float oldSalary = ptr->getSalary(); ptr->setSalary(newSalary); printf("Updated salary of employee from Old salary = %f to New salary = %f\n", oldSalary, ptr->getSalary()); } else if (choice == 2) { printf("Enter new manager first name\n"); cin >> newFirstName; printf("Enter new manager last name\n"); cin >> newLastName; employee *mgr = searchEmployeeHelper(newFirstName, newLastName); if (mgr != NULL) { employee *oldMgr = ptr->getManager(); oldMgr->deleteFromDirectReportingList(ptr); mgr->addToDirectReportingList(ptr); ptr->setManager(mgr); printf("Updated manager of employee from Old manager = %s to New manager = %s\n", oldMgr->getName().c_str(), ptr->getManagerName().c_str()); } } return true; } else { return false; } } // main function to update the employee details void employeeMgr::updateEmployeeDetails() { string firstName = getFirstName(); string lastName = getLastName(); employee* ptr = searchEmployeeHelper(firstName, lastName); if (ptr != NULL) { updateEmployeeDetailsHelper(ptr); } else { printf("Employee not found in database.\n"); } // return to main menu showMainMenu(); }*/ // The main menu to show the options available in the Employee Data Management system void employeeMgr::showMainMenu() { unsigned maxChoice = 0; if (_totalEmployees > 0) { printf("-----------------------------------------------------------\n"); printf("Please scroll up to view previous operation output\n"); printf("Total employees in the company = %d\n", _totalEmployees); printf("Add Employees from file: Press 0\n"); printf("Add Employee: Press 1\n"); printf("Search Employee: Press 2\n"); printf("Delete Employee: Press 3\n"); printf("Show All Employees Data: Press 4\n"); printf("Show Employee Hierarchy: Press 5\n"); printf("Exit Program: Press 6\n"); printf("-----------------------------------------------------------\n"); maxChoice = 7; } else { printf("-----------------------------------------------------------\n"); printf("No employees in the company yet\n"); printf("Add Employees from file: Press 0\n"); printf("Add Employee: Press 1\n"); printf("Exit Program: Press 2\n"); maxChoice = 3; } unsigned choice; if (choiceSelector(maxChoice, choice)) { if (choice == 0) { addEmployee(choice); } else if (choice == 1) { addEmployee(choice); } else if (choice == 2) { if (_totalEmployees > 0) { searchEmployee(); } else { printf("Exitting Employee Management Program\n"); return; } } else if (choice == 3) { deleteEmployee(); } else if (choice == 4) { showAllEmployeesData(); } else if (choice == 5) { showEmployeeHierarchy(); } else if (choice == 6) { printf("Exitting Employee Management Program\n"); return; } else if (choice == 7) { printf("Exitting Employee Management Program\n"); return; } } else { printf("Exitting Employee Management Program\n"); return; } }
### First employee is always Senior Most Employee in the company ### Senior Most Employee has Manager Name empty or NULL ### All other employees must have manager name which is an existing employee Employee First Name Employee Last Name Salary Manager First Name Manager Last Name Rahul Bhatia 1000.0 NULL Gaurav Singla 1000.0 Rahul Bhatia Puneet Goyal 900.0 Rahul Bhatia Rohit Sethi 800.0 Puneet Goyal

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue