Question
Here is my code so far, i can not get option 2 and 4 to execute properly. When I tell it to delete record it
Here is my code so far, i can not get option 2 and 4 to execute properly. When I tell it to delete record it does not seem to do anything and when i tell it to display records it just prints out 0s nonstop:
//Employee Data Base
// ATTENTION: As of right now i cant seem to get option 2 delete record and i can not get option 4 display records to work // Options 1, 3, 5, 6 all seem to be working fine
#include
class Employee { int id; char firstName[20], lastName[20]; char socialSecurityNumber[11]; //xxx-xxx-xxxx float salary; //Doesn't allow for commas to be entered when giving salary for example int age; //do 35000 instead of 35,000
public: void getdata(int i) { //read data from employee id = i + 1; cout << "First Name: "; cin >> firstName; cout << "Last Name: "; cin >> lastName; do { cout << "social Security Number (Enter in this format xxx-xxx-xxxx): "; cin >> socialSecurityNumber; // Enter in format xxx-xxx-xxxx } while (strchr(socialSecurityNumber, '-') == NULL);
cout << "Salary (no comma needed): "; cin >> salary; cout << "Age: "; cin >> age;
}
void printData() {//print employee information cout << id << "\t" << firstName << "\t" << lastName << "\t" << socialSecurityNumber << "\t" << salary << "\t" << age << " "; }
int getID() { return id; } char* getSSN() { return socialSecurityNumber; }
}emp1, emp;
void addRecord(fstream& fio, int count); bool searchbyID(fstream& fio, int id); bool searchbySSN(fstream& fio, char ssn[]); bool deleteRecord(fstream& fio, char ssn[]); void display(fstream& fio); int main() {
int count = 0; fstream fio("employeeInfo.dat", ios::in | ios::out); char ans = 'y'; int ch; bool flag = false; int id; char ssn[11]; while (!flag) {
cout << "Select 1. Add record \t2. Delete Record \t3. Search Record by ID \t4. Display records \t5. Exit \t6. Search Record by SSN (Enter in this format xxx-xxx-xxxx)" << endl; cout << " Enter choice: "; cin >> ch;//read choice switch (ch) { case 1://add record into file addRecord(fio, count); count++;//adding index break; case 2: cout << "Enter a social security number for delete record: (Enter in this format xxx-xxx-xxxx)"; cin >> ssn; //read sn for delete record if (deleteRecord(fio, ssn) == false) { cout << " Record not found in the file..!! "; cout << "Press any key to exit... "; exit(2); } break; case 3: cout << "Enter a employee ID for searching: "; cin >> id;//read employee id for search record in file if (searchbyID(fio, id) == false) { cout << " Record not found in the file..!! "; cout << "Press any key to exit... "; exit(2); } break; case 6:
cout << "Enter a social security number (Enter in this format xxx-xxx-xxxx): "; cin >> ssn;//read social security number for search record in file if (searchbySSN(fio, ssn) == false) { cout << " Record not found in the file..!! "; cout << "Press any key to exit... "; exit(2); } break; case 4: display(fio);//display emlpoyees information //this section is not working at all break; case 5: flag = true;//set flag to true to exit break; default: cout << "Enter valid choice only." << endl;
} } fio.close(); return 0; } void addRecord(fstream& fio, int count) { //add record into file emp1.getdata(count); fio.write((char*)&emp1, sizeof(emp1));//write record on file } bool searchbySSN(fstream& fio, char ssn[])//search by employee social Security Number { //search record in file int pos; fio.seekg(0); while (!fio.eof()) { pos = fio.tellg();//get positon of cursur fio.read((char*)&emp1, sizeof(emp1));//read record from file if (strncmp(emp1.getSSN(), ssn, 3) == 0)//campare ssn with emp1 ssn { emp1.printData();//print data fio.seekg(pos); fio.write((char*)&emp1, sizeof(emp1)); return true;//and return true is found } } return false;//return false if not found in the list } bool searchbyID(fstream& fio, int id)//search by Employee id { //search record in file int pos; fio.seekg(0); while (!fio.eof()) { pos = fio.tellg();//get positon of cursur fio.read((char*)&emp1, sizeof(emp1));//read record from file if (emp1.getID() == id)//campare id with emp1 id { emp1.printData();//print founded record fio.seekg(pos); fio.write((char*)&emp1, sizeof(emp1)); return true;//and return true is found } } return false; } bool deleteRecord(fstream& fio, char ssn[]) //this section does not seem to be working as of 5/5 { //search record in file int pos; fio.seekg(0); while (!fio.eof()) { pos = fio.tellg(); fio.read((char*)&emp1, sizeof(emp1));//read record from file if (strncmp(emp1.getSSN(), ssn, 2) == 0) { emp1.printData();//print founded record cout << "Employee record found" << endl; fio.seekg(pos); fio.write((char*)&emp1, sizeof(emp1)) << ""; return true;//and return true is deleded } } return false; }
void display(fstream& fio) { fio.seekg(0); cout << "Now the file contains: "; while (true) { fio.read((char*)&emp, sizeof(emp));//read record from file to emp if (fio.eof()) break; emp.printData();//print data of each employee } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started