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

The code I have is like this: #include <iostream> #include <string> #include <cs

ID: 3843360 • Letter: T

Question

The code I have is like this:

#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;

int main() {
int i = 0;
const int NUM_VALS = 7;
vector<string> firstname(NUM_VALS);
vector<string> lastname(NUM_VALS);
string searchval;

for (i = 0; i < NUM_VALS; ++i) {
cin >> firstname.at(i);
cin >> lastname.at(i);
}

cin >> searchval;

for (i = 0; i < NUM_VALS; ++i) {
if (searchval == lastname.at(i)){
cout << firstname.at(i);
}
}

return 0;
}

--------------------------

The results are all good except for this one:

2. Compare output:

John

Doe

Steve

Smith

Smith

Smith

Steve

Input:

John

Doe

Steve

Smith

Smith

Your output:

Smith

Expected output:

Steve

5.23 Day 15: Put Smallest First 5.24 Programming 5: Find First Name Students This content is controlled by your instructor, and is not zyBooks content. Direct questions or concerns about this content to your instructor. If you have any technical issues with the zyLab submission system, use the "Trouble with lab?" button at the bottom of the lab. This programming assignment will build on lab 7 As before you wi read in a list of first and last names (ending the list with a blank line This time, you wi read an additional last name. You wi need to write code to find the first name of the person with the last name read. (Assume for this test there will be only one). Print the first name for the person found. Example input and Output below: INPUT John Doe Steve Smith Smith OUTPUT Steve

Explanation / Answer

I dont understand why you are always reading 7 Times,

But looks like you are putting values into vector much more than its needed.

You can do a break and dont print it again, Looks like that could be the Problem .

=======================================================================================

#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
int main() {
int i = 0;
const int NUM_VALS = 7;
vector<string> firstname(NUM_VALS);
vector<string> lastname(NUM_VALS);
string searchval;

for (i = 0; i < NUM_VALS; ++i) {
cin >> firstname.at(i);
cin >> lastname.at(i);
}

cin >> searchval;

for (i = 0; i < NUM_VALS; ++i) {
if (searchval == lastname.at(i)){
cout << firstname.at(i);
break;
}
}

return 0;
}


===========================================================================
Let me know if it works, If not I can modify and not read input always 7 times as you are doing.
Thanks, let me know if there is anything.

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