How do I write a code to find a person by name and print out all the info I stored about the person? I will post a pictu

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

How do I write a code to find a person by name and print out all the info I stored about the person? I will post a pictu

Post by answerhappygod »

How do I write a code to find a person by name and print
out all the info I stored about the person? I will post a picture
of my code since its too big sorry for the trouble!
How Do I Write A Code To Find A Person By Name And Print Out All The Info I Stored About The Person I Will Post A Pictu 1
How Do I Write A Code To Find A Person By Name And Print Out All The Info I Stored About The Person I Will Post A Pictu 1 (101.58 KiB) Viewed 54 times
1 #include <iostream> 2 #include <algorithm> 3 #include <vector> 4 #include <iomanip> 5 #include "Person.h" 6 #include "Laborer.h" 7 #include "Manager.h" 8 #include "Owner.h" 9 using namespace std; 10 void menu(); 11 void subMenu1(vector<Person* >&); 12 void subMenu2 (vector<Person* >&); 13 void SortPersons (vector<Person*>&); 14 - int main() { 15 int choice; 16 // declare a vector of Person 17 vector<Person*> persons; 18 do { 19 cout << "1. Add person" << endl; 20 cout << "2. Display list" << endl; 21 cout << "3. Sort list" << endl; 22 cout << "4. Quit" << endl; 23 cout << "Enter your choice: "; 24 cin >> choice; 25 switch (choice) 26 { 27 case 1:; 28 subMenu1 (persons); 29 break; 30 case 2:; 31 subMenu2 (persons); 32 break; 33 case 3:; 34 SortPersons (persons); 35 break; 36 case 4:; 37 break; 38 default: 39 cout << "Invalid choice" << endl; 40 break; 41 } 42 43 } 44 while(choice != 4); 45 return 0; 46 } 47 void subMenu1(vector<Person*>& persons) { 48 cout << "1. Owner" << endl; 49 cout << "2. Manager" << endl; 50 cout << "3. Laborer" << endl; 51 int choice; 52 cout << "Please choose your choice: "; 53 cin >> choice; 54 cin.ignore(); 55 switch(choice) { 56 case 1: { 57 string name, ssn, birthDate, ownDate; 58 double businessPercentage; 59 cout << "Please enter the name: "; 60 getline(cin, name); 61 cout << "Please enter the SSN: "; 62 getline(cin, ssn); 63 cout << "Please enter the birth date: "; 64 getline(cin, birthDate);

5858882乃乃形 66 67 68 69 70 71 72 cout << "Please enter how much business Percentage own: "; cin >> businessPercentage; cin.ignore(); cout << "When did you became an owner: "; getline(cin, ownDate); Owner owner * new Owner(); owner->setName(name); owner->setSsn(ssn); owner->setBirthDate(birthDate); owner->setBusinessPercentage(businessPercentage); owner->setOwnerDate(ownDate); persons.push_back(owner); 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 cout << "Owner info added Successfully" << endl; break; } } case 2: { string name, ssn, birthDate, department, empId; double salary; cout << "Please enter the name: "; getline(cin, name); cout << "Please enter the SSN: "; getline(cin, ssn); cout << "Please enter the birth date: "; getline(cin, birthDate); cout << "Please enter the department: "; getline(cin, department); cout << "Please enter the employee id: "; getline(cin, empId); cout << "Please enter the salary: "; cin >> salary; cin.ignore(); Manager* man = new Manager(); man->setName (name); man-> setssn(ssn); man->setBirthDate(birthDate); man->setDepartment(department); man->setEmpId(empId); man->setSalary(salary); persons.push_back(man); cout << "Manager info Added Successfully" << endl; break; } case 3: { string name, ssn, birthDate, job, empId; double hourlySalary, hoursWorked; cout << "Please enter the name: "; " getline(cin, name); cout << "Please enter the SSN: "; getline(cin, ssn); cout << "Please enter the birth date: "; getline(cin, birthDate); cout << "Please enter the job: "; getline(cin, job); cout << "Please enter the employee id: "; getline(cin, empId); cout << "Please enter the hourly salary: "; cin >> hourlySalary; cin.ignore(); cout << "Please enter the hours worked: "; cin >> hoursWorked; Laborer* lab = new Laborer(); lab->setName(name);

157 - 11 129 lab->setssn(ssn); 130 lab->setBirthDate(birthDate); 131 lab->setJob(job); 132 lab->setEmpId(empId); 133 lab->setHourlySalary(hourlySalary); 134 lab->setHoursWorked (hoursWorked); 135 persons.push_back(lab); 136 cout << "Laborer info Added Successfully" << endl; 137 break; 138 } 139 default: 140 cout << "Invalid choice" << endl; 141 break; 142 } 143 } 144 void subMenu2 (vector<Person* >& persons) { 145 cout << "1. All" << endl; 146 cout << "2. Owners" << endl; 147 cout << "3. Managers" << endl; 148 cout << "4. Laborers" << endl; 149 int choice1; 150 cout << "Enter your choice: \n"; 151 cin >> choice1; 152 cin.ignore(); 153 switch (choice1) 154 { 155 case 1: 156 for (int i = 0; i < persons.size(); i++) { < i if (persons->category() == "Owner") { 158 Owner own dynamic_cast Owner*> (persons); 159 cout << "Category: " << persons-category() << endl; " 160 cout << "Name: << persons- getName() << endl; 161 cout << "SSN: " << persons->getSsn() << endl; " 162 cout << "BirthDate: " << persons- getBirthDate() << endl; 163 cout << "business Percentage: << own- getBusinessPercentage() << endl; 164 cout << "Owner Date: " << own->getOwnerDate() << endl; 165 cout << << endl; 166 } 167- else if (persons->category() == "Manager") { i 168 Manager man = dynamic_cast Manager*> (persons); 169 cout << "Category: " << persons->category() << endl; 170 cout << "Name: " << persons.getName() << endl; 171 cout << "SSN: " << persons[i]->getSsn() << endl; 172 cout << "BirthDate: " << persons[i]- getBirthDate() << endl; 173 cout << "Department: " << man->getDepartment() << endl; 174 cout << "Employee Id: " << man-getEmpId() << endl; > 175 cout << "Salary: " << man- getSalary() << endl; 176 cout << "- << endl; 177 } 178 else 179 { 180 Laborer lab dynamic_cast<Laborer*>(persons[i]); 181 cout << "Category: << persons[i]->category() << endl; 182 cout << "Name: " << persons[i].getName() << endl; : - 183 cout << "SSN: " << persons[i]->getSsn) << endl; 184 cout << "BirthDate: " << persons[i].getBirthDate() << endl; 185 cout << "Job: " << lab->getJob() << endl; 186 cout << "Employee Id: " << lab->getEmpId() << endl; " 187 cout << "Hourly salary: " << lab-getHourlySalary() << endl; > 188 cout << "Hours Worked: " << lab->getHoursWorked) << endl; ( 189 cout << << endl; 190 } 191 } 192 break; 11 F

194 for (int i = 0; i < persons.size(); i++) { 195 if (persons[i]->category() == "Owner") { 196 Owner* own dynamic_cast Owner*> (persons[i]); 197 cout << "Category: << persons[i]->category() << endl; 198 cout << "Name: " << persons[i]->getName() << endl; 199 cout << "SSN: " << persons[i]->getSsn() << endl; 200 cout << "BirthDate: << persons[i]->getBirthDate() << endl; 201 cout << "business Percentage: " << own- getBusinessPercentage() << endl; 202 cout << "Owner Date: << own->getOwnerDate() << endl; 203 cout << << endl; 204 } 205 } 206 break; 207 case 3: 208 for (int i = 0; i < persons.size(); i++) { 209 if (persons[i]->category() == "Manager") { 210 Manager* man = dynamic_cast Manager*>(persons[i]); 211 cout << "Category: " << persons[i]->category() << endl; 212 cout << "Name: " << persons[i].getName() << endl; 213 cout << "SSN: " << persons[i]->getSsn) << endl; 214 cout << "BirthDate: " << persons[i]->getBirthDate() << endl; 215 cout << "Department: " << man-getDepartment() << endl; 216 cout << "Employee Id: " << man->getEmpId() << endl; 217 cout << "Salary: " << man- getSalary() << endl; 218 cout << << endl; 219 } 220 } 221 break; 222 case 4: 223 for (int i = 0; i < persons.size(); i++) { 224 if (persons[i]->category() "Laborer") { 225 Laborer* lab = dynamic_cast<Laborer*>(persons[i]); 226 cout << "Category: " << persons[i]->category() << endl; 227 cout << "Name: " << persons[i]->getName() << endl; 228 cout << "SSN: << persons[i]->getSsn) << endl; 229 cout << "BirthDate: " « persons[i]-getBirthDate() << endl; 230 cout << "Job: " << lab->getJob() << endl; 231 cout << "Employee Id: " << lab->getEmpId() << endl; 232 cout << "Hourly salary: " << lab->getHourlySalary() << endl; 233 cout << "Hours Worked: " << lab->getHoursWorked() << endl; 234 cout << << endl; 235 } 236 } 237 break; 238 default: 239 break; 240 } 241} 242 243 void SortPersons (vector<Person* >& persons) { 244 // sort by name 245 sort(persons.begin(), persons.end(), [](Person* a, Person* b) { 246 return a->getName() < b->getName(); 247 }); 248 cout << "Sorted by name successfully" << endl; 249
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply