Search Function for the Speaker's Bureau Program

Hey guys,

I'm working on this program for homework an here is the problem:

Write a program that keeps track of a speakers bureau. The program should use a structure to store the following data about a speaker.

Name
Telephone Number
Speaking Topic
Fee required

The program should use an array of at least 10 structures.It should let the user enter data into the array, change the contents of any element, and display all the data stored in the array. The program should have a menu-driven user interface.

So I did the program which is below but now it is asking this:

Add a function that allows the user to search for a speaker on a particular topic. It should accept a key word as an argument and then search the array for a structure with that key word in the searching topic field. All structures that match should be displayed. If no structure matches, a message saying so should be displayed.

Now this where I get stuck, I'm having trouble figuring out how to write down the search function in this program. A function that can work and how to get it to match the topics listed by each speaker.

If anyone can Please help me out

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct speakerBureau
{
string name;
string TelephoneNumber;
string SpeakTopic;
int fee;
};

void getSpeaker(speakerBureau *);
void printSpeaker(speakerBureau *);
void editSpeaker(speakerBureau *);
void searchSpeakTopic(speakerBureau*);

int main()
{
int NUM_SPEAKERS = 10;
int index;
speakerBureau info[10];


int menu;
const int enter = 1,
change = 2,
print = 3,
search = 4,
leave = 5;


do{
cout << "Please select a choice from the menu.\n"
<< "1) Enter Speaker Information.\n"
<< "2) Change Speaker Information.\n"
<< "3) Print Speaker Information.\n"
<< "4) Search for Topic. \n"
<< "5) Leave this menu.\n"
<< "Select: ";
cin >> menu;

switch (menu)
{
case enter:
{

getSpeaker(info);
}
break;
case change:
{

editSpeaker(info);
}
break;
case print:
{

printSpeaker(info);
}
break;

case search:
{
searchSpeakTopic(info);
}
}
} while (menu != leave);

system("pause");
return 0;
}
void getSpeaker(speakerBureau *p) //array name = pointer
{


int i = 0;
int size = 10;
for (i = 0; i < size; i++)
{
cout << "Please enter the following information of speaker " << i << " : \n";
cout << "Speaker Name:";
cin.ignore();
getline(cin, p[i].name);
cout << "\nSpeaker Telephone Number:";
cin.ignore();
getline(cin, p[i].TelephoneNumber);
cout << "\nSpeaker Topic:";
cin.ignore();
getline(cin, p[i].SpeakTopic);
cout << "\nFee Required:";
cin >> p[i].fee;
}
}

void printSpeaker(speakerBureau *p)
{
int i = 0;
int size = 10; // Array size
for (i = 0; i < size; i++)
{
cout << "The information entered for each speaker is: \n";
cout << "Speaker " << i << endl;
cout << "Speaker Name: " << p[i].name << endl;
cout << "Speaker Telephone Number: " << p[i].TelephoneNumber << endl;
cout << "Speaker Topic: " << p[i].SpeakTopic << endl;
cout << "Speaker Fee Required: " << p[i].fee << endl;
}
}


void editSpeaker(speakerBureau *p)
{
int i;
cout << "Please enter the number of the speaker you would like to edit."
<< endl;
cin >> i;
if (i <= 9)
{
cout << endl;
cout << "Please enter the updated information of the speaker: \n";
cout << "Speaker Name:";
cin.ignore();
getline(cin, p[i].name);
cout << "\nSpeaker Telephone Number:";
getline(cin, p[i].TelephoneNumber);
cout << "\nSpeaker Topic:";
getline(cin, p[i].SpeakTopic);
cout << "\nFee Required:";
cin >> p[i].fee;
}
else
{
cout << "Please pick a number between 0-9" << endl;

}
}

void searchSpeakTopic(speakerBureau*p)
{
int i = 0;
int topic;

cout << " Please type a topic in the program" << endl;

cin >> topic;


}
topic should be a string, not an int - iterate through your array of speakerBureau (info[]) and search for the topic in each SpeakTopic member.

Please note that you have guaranteed all 10 elements of the array have been initialised.

A rough idea would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void searchSpeakTopic(speakerBureau p[]) // original is okay, just want to emphasize that p is an array
{
    string topic;
    cout << " Please type a topic (case sensitive) in the program" << endl;
    cin >> topic;
    bool speakerFound = false;
    for (int i = 0; i < sizeof(p) / sizeof(p[0]); ++i)
    {
        if (p[i].SpeakTopic.find(topic) != string::npos)
        {
            speakerFound = true;
            printSpeaker(&(p[i]));
        }
    }

    if (!speakerFound)
    {
        cout << "No speakers spoke about " << topic << endl;
    }
}


In main, you'd call it like searchSpeakTopic(info);

I didn't compile this, so don't know if I made any mistakes, it's just to give a rough idea.
it appears that it can run through fine in he program but when i type in a topic it said's no one spoke about it but one of the speakers did so it still won't read it.
could you post your session - all the input and output, for testing and comparison.
My professor actualy wants me to use linear search or binary search. So I need some help how I could apply it to this structure program
Topic archived. No new replies allowed.