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

Doing homework assignment in my programming 2 class and every time I run the pro

ID: 3862387 • Letter: D

Question

Doing homework assignment in my programming 2 class and every time I run the program is never finds anything that I am trying to look up. What do I have in the wrong place

#include <iostream>

#include <string>

#include <cstring>

using namespace std;

int main()

{

const int Num_People = 10;

const int Length = 27;

string people[Num_People] = { "Becky Warren, 555-1223",

"Geri Palmer, 555-8787",

"Lynn Presnell, 555-1212",

"Holly Gaddis, 555-8878",

"Sam Wiggins, 555-0998",

"Bob Kain, 555-8712",

"Tim Haynes, 555-7676",

"Warren Gaddis, 555-9037",

"Jean James, 555-4939",

"Ron Palmer, 555-2783" };

char lookUp[Length];

size_t strPos;

int index,count=0;

cout << " People and Phone Numbers ";

//Prompt the user to enter name or partial name

cout << "Enter a name or partial name to search for: ";

//read name or partial name

cin.getline(lookUp, 27);

//Find the name in all string objects

for (index = 0; index < 10; index++)

{

//if the name is found in the string, the starting

//position of name is returned into strPos

strPos = people[index].find(lookUp);

//strPos equal to no postions, if the name is

//not found in the string

if (strPos != string::npos)

{

//Print the string, in which the entered name is found

cout << people[index] << endl;

count++;//Increase the count

}

}

//count will be 0,if there are no matchings

if (count == 0)

cout << "No matching was found. ";
   system ("pause");

return 0;

Explanation / Answer

Hi

I have modified the code and highlighted the code changes below. It is working as expected.

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
const int Num_People = 10;
const int Length = 27;
string people[Num_People] = { "Becky Warren, 555-1223",
"Geri Palmer, 555-8787",
"Lynn Presnell, 555-1212",
"Holly Gaddis, 555-8878",
"Sam Wiggins, 555-0998",
"Bob Kain, 555-8712",
"Tim Haynes, 555-7676",
"Warren Gaddis, 555-9037",
"Jean James, 555-4939",
"Ron Palmer, 555-2783" };
string lookUp;
size_t strPos;
int index,count=0;
cout << " People and Phone Numbers ";
//Prompt the user to enter name or partial name
cout << "Enter a name or partial name to search for: ";
//read name or partial name
getline(cin, lookUp);
//Find the name in all string objects
for (index = 0; index < 10; index++)
{
  
if (people[index] == lookUp)
{
//Print the string, in which the entered name is found
cout << people[index] << endl;
count++;//Increase the count
}
}
//count will be 0,if there are no matchings
if (count == 0)
cout << "No matching was found. ";
system ("pause");
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

        People and Phone Numbers                                                                                                                                                                                                                                       

                                                                                                                                                                                                                                                                       

Enter a name or partial name to search for: Ron Palmer, 555-2783                                                                                                                                                                                                       

Ron Palmer, 555-2783

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