Question: Problem description ?GIVEN CODES BELOW QUESTION Port the CODES from C to C++. employee list.cpp is already done. Replace all of the C code in
Problem description
?GIVEN CODES BELOW QUESTION
Port the CODES from C to C++. employee list.cpp is already done. Replace all of the C code in libel.c and libel.h with C++ code. Rename libel.c to libpel.cpp and libel.h to libel.hpp. Convert the personal_info structure into a class with three private data member strings for the ?rst name, last name, and position and one private data member double for the salary. To access private members of a class, there are typically public get and set functions. Get/set member function prototypes for the data members of the class personal_info are shown below (to be added into your libel.hpp ?le). There is a get and a set for each private data member. A set function will set the private member equal to the functions input. A get function will return the private members value. This is typically good practice for things that should not be accessed directly (think of them as hidden from the end user).
Public member function prototypes for the personal info class: void setFirst(string first);
void setLast(string last);
void setPosition(string position);
void setSalary(double salary);
string getFirst(); string getLast();
string getPosition(); double getSalary();
Convert struct employee_list to a class with a private member: vector
void save el(string filename);
void add person(personal info person);
void search el(string find name);
void gen csv sal();
void gen csv pos();
int get num people(); Note 1: Remember to scope your member functions when writing the de?nitions in libel.cpp by using the scope resolution operator :: as we have learnt in lecture 11 for classes/objects/members. Hints: add_person(person) must push the new personal_info class onto the vector of employee_list, as opposed to assigning the structure to a position in the array in the previous C code. Note that, we do NOT need to de?ne or limit the size of the phonebook here, since the size of a vector in C++ can grow as needed. Change all printf statements to cout statements, and all scanf statements to cin statements. Change the fprintf and fscanf statements accordingly as well. The references to other data members of the class must also be changed accordingly. Note 2: The ?lename generator function needs to be declared and de?ned as an independent function (not part of any classes) with the following prototype in the header ?le: string gen_file name(string filename, string suffix); Make the necessary changes in the library ?le for the de?nition of gen file name function. Note 3: Only employee list.cpp and a sample directory.txt are provided. You need to write the libpel.cpp and libel.hpp completely by yourself. Read the code in employee_list.cpp carefully and write the library ?les accordingly (you do NOT need to change anything in employee_list.cpp). All the functionalities of the menu should be exactly the same as your C codes
emlpoyee_list.cpp( C++)
#include "libel.hpp"
int main () { string filename = "directory.txt"; string csv_prefix = "csv_list"; string find_name; personal_info person; employee_list emp_list; string first, last, position; double salary;
emp_list.load_el(filename); cout << emp_list.get_num_people() << " employees loaded." << endl; char cont = 'x', //initialize continue to something other than 'Y' or 'N' tmp; // int state = -1, correct_search_val; string srch_critera = "init";
while (state != 4){ while(state < 1 || state > 4){ cout << "1:\tAdd employee and salary "; cout << "2:\tSearch directory by first name "; cout << "3:\tGenerate CSV "; cout << "4:\tSave and exit program "; cout << "Enter an option (1-4): "; cin >> state; if(state < 1 || state > 4) { cout << " Error: number not in range "; } }
switch(state) {
//add employee and salary case 1: //add one person cout << "Enter a first name: "; cin >> first; cout << "Enter " << first << "'s last name: "; cin >> last; cout << "Enter " << first << "'s occupation: "; cin >> position; cout << "Enter " << first << "'s Salary: "; cin >> salary; person.setFirst(first); person.setLast(last); person.setPosition(position); person.setSalary(salary); emp_list.add_person(person); cout << "Employee added. "; //determine to add more people to the employee list while(cont != 'N' && emp_list.get_num_people() < MAXPPL) { cont = 'x'; while(cont != 'Y' && cont != 'N'){ cout << "Would you like to enter another name (Y/N): "; cin >> cont; if (cont != 'Y' && cont != 'N') { cout << "Error: User entered " << cont << ". Must enter either 'Y' or 'N' "; } } if(cont != 'N') { cout << "Enter a first name: "; cin >> first; cout << "Enter " << first << "'s last name: "; cin >> last; cout << "Enter " << first << "'s occupation: "; cin >> position; cout << "Enter " << first << "'s Salary: "; cin >> salary; person.setFirst(first); person.setLast(last); person.setPosition(position); person.setSalary(salary); emp_list.add_person(person); cout << "Employee added. "; } } cout << " Returning to main menu... "; state = -1; break;
//search directory by first name case 2:
cont = 'x'; //reset continue to neither 'Y' nor 'N' while(cont != 'N'){ cont = 'x'; cout << "Enter a person's name to search for: "; cin >> find_name; emp_list.search_el(find_name); while(cont != 'Y' && cont != 'N'){ cout << " Continue (Y/N)? "; cin >> cont; if (cont != 'Y' && cont != 'N') { cout << "Error: User entered " << cont << ". Must enter either 'Y' or 'N'. "; }
} } cout << " Returning to main menu... "; state = -1; break;
//generate CSV file case 3: correct_search_val = -1; while(correct_search_val != 0) { cout << "Generate CSV based on? (\"Salary\", \"Position\"): "; cin >> srch_critera; if(srch_critera == "Salary"){ cout << "Generating CSV based on salary..." << endl; emp_list.gen_csv_sal(); correct_search_val = 0; } else if(srch_critera == "Position"){ cout << "Generating CSV based on position..." << endl; emp_list.gen_csv_pos(); correct_search_val = 0; } else cout << "Options are: \"Salary\", \"Position\" "; } cout << "Returning to main menu... "; state = -1;
case 4: break; }//end switch }//end while
//save the employee list emp_list.save_el(filename); cout << emp_list.get_num_people() << " employees saved." << endl; return 0;
}//end main
libel.h
#ifndef LIBCL_H #define LIBCL_H #define MAXPPL 500 #define MAXLEN 25
//ADD STRUCTURE(S) HERE struct personal_info { char first[MAXLEN]; char last[MAXLEN]; char position[MAXLEN]; double salary; }; typedef struct personal_info pi;
struct employee_list{ pi person[MAXPPL]; int num_people; }; typedef struct employee_list el;
//ADD PROTOTYPES HERE void load_el(el * emp_list,const char * filename); void add_person(el * emp_list, pi person); void search_el(el emp_list,char find_name[]); void save_el(el * emp_list,const char * filename); void gen_csv_sal(el * emp_list); void gen_csv_pos(el * emp_list); char * gen_file_name(char * filename, int filename_size, char * suffix, int suffix_size); #endif
LIBEL.C
#include "libel.h" #include
//ADD FUNCTION DEFINITIONS FOR LOAD_EL, SAVE_EL, ADD_PERSON, AND SERACH_EL HERE void load_el(el * emp_list, const char * filename){
//char first[30],last[30],occupation[30],ch; //double salary; //int employees_loaded,num_people; FILE * fp = fopen(filename,"r"); fscanf(fp,"%d",&emp_list->num_people);
for(int i = 0; i < emp_list->num_people; i++){
fscanf(fp,"%s %s %s %lf",emp_list->person[i].first,emp_list->person[i].last,emp_list->person[i].position,&emp_list->person[i].salary);
} fclose(fp); }
void add_person(el * emp_list, pi person){ int count = emp_list->num_people; strcpy(emp_list->person[count].first,person.first); strcpy(emp_list->person[count].last,person.last); strcpy(emp_list->person[count].position,person.position); emp_list->person[count].salary= person.salary;
count++; emp_list->num_people=count;
}
void search_el(el emp_list,char find_name[]){
int i; for(i=0;i<(emp_list.num_people);i++){ if(strcmp(find_name,emp_list.person[i].first)==0){
printf(" Name: %s %s (%s) ",emp_list.person[i].first,emp_list.person[i].last,emp_list.person[i].position); printf("Salary: %.2lf ",emp_list.person[i].salary); } } }
void save_el(el * emp_list, const char * filename){
char first[30],last[30],occupation[30],c; int salary,num_people, employees_loaded=0; FILE * fp = fopen(filename,"w"); //c = getc(fp); fprintf(fp, "%d ", emp_list->num_people); for(int i = 0; i < emp_list->num_people; i++){ fprintf(fp,"%s %s %s %lf ",emp_list->person[i].first, emp_list->person[i].last, emp_list->person[i].position, emp_list->person[i].salary); employees_loaded++; } // emp_list->num_people= employees_loaded; fclose(fp); }
//ADD FUNCTIONALITY TO THE BELOW FUNCTIONS
void gen_csv_sal(el * emp_list){ char csv_prefix[14] = "csv_list_sal"; char csv_suffix[5] = ".csv"; char * filename = gen_file_name(csv_prefix, 14, csv_suffix, 5); FILE * fp = fopen(filename, "w"); int search = -1; double sal; char ml[5];
while(search == -1) { printf("Search for salaries (format: 65000.00 less or 100000 more): "); scanf("%lf %s", &sal, ml); if(strcmp(ml, "less") == 0) { int i; for(i=0;i
//ADD CODE HERE TO PRINT TO THE CSV FILE AND SCREEN
search = 0; } else if(strcmp(ml, "more") == 0) { int i; for(i=0;i
printf("%s,%s,%s,%lf ",emp_list->person[i].first,emp_list->person[i].last,emp_list->person[i].position,emp_list->person[i].salary); } }
//AD CODE HERE TO PRINT TO THE CSV FILE AND SCREEN
search = 0; } else printf("Must type more or less "); }
fclose(fp); printf("CSV generated: %s ", filename); return; }
//ADD FUNCTIONALITY TO THE BELOW FUNCTION
void gen_csv_pos(el * emp_list){ char csv_prefix[14] = "csv_list_pos"; char csv_suffix[5] = ".csv"; char * filename = gen_file_name(csv_prefix, 14, csv_suffix, 5); FILE * fp = fopen(filename, "w"); char pos[25]; printf("Enter company position to search for: "); scanf("%s", pos); int i; for(i=0;i
fclose(fp); printf("CSV generated: %s ", filename); return; }
char * gen_file_name(char * filename, int filename_size, char * suffix, int suffix_size){ char file_num[3]; int max_file_num = 99, i; char * new_filename = (char *) malloc((filename_size + suffix_size + 2) * sizeof(char)); for(i = 0;i <= max_file_num; i++){ strcpy(new_filename, filename); sprintf(file_num, "%d", i); strcat(new_filename, file_num); strcat(new_filename, suffix); if (fopen(new_filename, "r") ==NULL) {//if the filename doesnt exsist //fp = fopen(new_filename, "w"); break; } } // printf("File name generated: %s ", new_filename); return new_filename; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
