Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is in C++. If you can show me a different way to write this and comment wah

ID: 3869052 • Letter: T

Question

This is in C++. If you can show me a different way to write this and comment waht it does. Thank you.

void search(const std::string& fsearch, const std::string& lsearch, const PeopleList& p1, PeopleList& result) {

if (fsearch == "*" && lsearch == "*") {

for (int i = 0; i < p1.size(); i++) {

std::string firstName;

std::string lastName;

InfoType p1Value;

p1.get(i, firstName, lastName, p1Value);

result.add(firstName, lastName, p1Value);

}

}

else if (fsearch == "*" && lsearch != "*") {

for (int j = 0; j < p1.size(); j++) {

std::string firstName;

std::string lastName;

InfoType p1Value;

p1.get(j, firstName, lastName, p1Value);

if (lastName==lsearch) {

result.add(firstName, lastName, p1Value);

}

}

}

else if (fsearch != "*" && lsearch == "*") {

for (int j = 0; j < p1.size(); j++) {

std::string firstName;

std::string lastName;

InfoType p1Value;

p1.get(j, firstName, lastName, p1Value);

if (firstName == fsearch) {

result.add(firstName, lastName, p1Value);

}

}

}

else {

if (p1.contains(fsearch,lsearch)) {

InfoType p1Value;

p1.lookup(fsearch, lsearch, p1Value);

result.add(fsearch, lsearch, p1Value);

}

else{

return;

}

}

}

Explanation / Answer

void search(const std::string& fsearch, const std::string& lsearch, const PeopleList& p1, PeopleList& result) {

if (fsearch == "*" && lsearch == "*") {

for (int i = 0; i < p1.size(); i++) {

std::string firstName;

std::string lastName;

InfoType p1Value;

p1.get(i, firstName, lastName, p1Value);

result.add(firstName, lastName, p1Value);

}

}

else if (fsearch == "*" && lsearch != "*") {

for (int j = 0; j < p1.size(); j++) {

std::string firstName;

std::string lastName;

InfoType p1Value;

p1.get(j, firstName, lastName, p1Value);

if (lastName==lsearch) {

result.add(firstName, lastName, p1Value);

}

}

}

else if (fsearch != "*" && lsearch == "*") {

for (int j = 0; j < p1.size(); j++) {

std::string firstName;

std::string lastName;

InfoType p1Value;

p1.get(j, firstName, lastName, p1Value);

if (firstName == fsearch) {

result.add(firstName, lastName, p1Value);

}

}

}

else {

if (p1.contains(fsearch,lsearch)) {

InfoType p1Value;

p1.lookup(fsearch, lsearch, p1Value);

result.add(fsearch, lsearch, p1Value);

}

else{

return;

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote