The program should ask the user to enter a name or partial name to search for in
ID: 3628622 • Letter: T
Question
The program should ask the user to enter a name or partial name to search for in the array. Any entries in the array that match the string entered should be displayed. For example, if the user enters “Palmer” or "Palm" the program should display the following names from the list:Geri Palmer, 223-8787
Ron Palmer, 486-2783
This are the code I have typed:
#include <iostream>
//#include <string>
//#include <cstdlib>
#include <cctype>
using namespace std;
void searchString(char pplInfo[length][width], int arrSize);
int main()
{
char pplInfo[length][width] = {"Becky Warren, 578-1223",
"Joe Looney, 586-0097",
"Geri Palmer, 223-8787",
"Lynn Presnell, 887-1212",
"Holly Gaddis, 223-8878",
"Sam Wiggins, 486-0998",
"Bob Kain, 586-8712",
"Tim Haynes, 586-7676",
"Warren Gaddis, 223-9037",
"Jean James, 678-4939",
"Ron Palmer, 486-2783" };
const int arrSize=12;
searchString(pplInfo, arrSize);
}
void searchString(char pplInfo[length][width], int arrSize)
{
char *searchChar[width]="none";
cout << "Enter name to search: ";
cin >> searchChar;
for(int count=0; count<=arrSize; count++)
{
if(strstr(pplInfo[count], searchChar));
{
cout << pplInfo[count] ;
}
}
}
Explanation / Answer
please rate - thanks
try this - no pointers
#include <iostream>
#include <string>
#include<ctype.h>
using namespace std;
string toUpper(string);
int main()
{int num=11,count=0,i,n;
string::size_type found;
string name,temp,temp2;
string phone[]={"Becky Warren, 678-1223","Jeo Looney, 586-0097",
"Geri Palmer, 223-8787","Lynn presnell, 887-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, 486-2783"};
cout<<"enter a name or partial name to search for: ";
getline(cin,name);
temp=toUpper(name);
cout<<" Names and numbers found ";
for(i=0;i<num;i++)
{temp2=toUpper(phone[i]);
found=temp2.find(temp);
if (found<temp2.size())
{cout<<phone[i]<<endl;
count++;
}
}
if(count==0)
cout<<" Sorry no name with "<<name<<" in your phone list ";
system("pause");
return 0;
}
string toUpper(string s)
{int i;
for(i=0;i<s.length();i++)
s[i] = toupper(s[i]);
return s;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.