Tuesday, June 6, 2017

Programming Challenge 11.10 - Search Function For The Speaker's Bureau Program

 Include File: "UtilityCls.h"



/* Search Function For The Speaker's Bureau Program - This program is a
    modification of Programming Challenge 11.9. It allows the user to
    search for a speaker on a particular topic. It accepts a key word as
    an argument and then searches the array for a structure with that key
    word in the Speaking Topic field. All structures that match are displayed.
    If no structure matches, a message saying so is displayed. */

#include "UtilityCls.h"

struct SpeakerInfo
{
    string name;                /* The speaker's name                 */
    string phoneNum;            /* The speaker's telephone number */
    string speakingTopic;    /* The speaking topic                 */
    double fee;                   /* The speaker's fee                     */   
};

enum menuSelection
{
    GET_SPEAKER_INFO = 1,
    CHANGE_SPEAKER_INFO = 2,
    SEARCH_TOPICS = 3,
    DISPLAY_SPEAKER_INFO = 4,
    QUIT = 5
};

enum dataMode
{
    GET_INFO = 1,
    CHANGE_INFO = 2
};

void menu(SpeakerInfo [], const int);
int  menuItems();
void getSpeakerInfo(SpeakerInfo [], int, const int);
int  getSpeakerID(SpeakerInfo [], const int);
void searchTopic(SpeakerInfo [], const int);
void displaySpeakerInfo(SpeakerInfo [], const int);

int main()
{
    const int NUM_SPEAKERS = 10;

    SpeakerInfo speaker[NUM_SPEAKERS];

    menu(speaker, NUM_SPEAKERS);

   pauseSystem();
   return 0;
}

/* **********************************************************
    Definition: menuItems

    This function presents a menu-screen to the user from
    which he or she can select one of the available options.
    ********************************************************** */

int menuItems()
{
    int menuItem = 0;

    cout << "\n\t\tSPEAKER'S CORNER - SPEAKER INFORMATION\n\n"
          << "\tMenu Selection\n\n"
          << "\t1. Enter Speaker Info\n"
          << "\t2. Change Speaker Info\n"
          << "\t3. Search Speaker Topic\n"
          << "\t4. Display Speaker Info\n"
          << "\t5. Quit\n\n"
          << "\tYour Selection: ";
    cin >> menuItem;

    while (menuItem < GET_SPEAKER_INFO || menuItem > QUIT)
    {
        cout << "\n\tMenu item " << menuItem << " does not exist.\n\n"
              << "\tYour selection: ";
        cin >> menuItem;
    }

    return menuItem;
}

/* **********************************************************
   Definition: menu

    This function represents the main-menu, from which all the
    other functions in this program are called.
   ********************************************************** */

void menu(SpeakerInfo speaker[], const int NUM_SPEAKERS)
{
    int selection = 0;
    int mode = 0;

    do
    {
        selection = menuItems();

        switch (selection)
        {
            case GET_SPEAKER_INFO:
            {
                clearScreen();
                cout << "\n\tSPEAKER'S CORNER - ENTER SPEAKER INFORMATION\n\n\n";

                mode = GET_INFO;
                getSpeakerInfo(speaker, mode, NUM_SPEAKERS);
            } break;

            case CHANGE_SPEAKER_INFO:
            {
                clearScreen();

                cout << "\n\tSPEAKER'S CORNER - CHANGE SPEAKER INFORMATION\n\n";
                mode = CHANGE_INFO;
                getSpeakerInfo(speaker, mode, NUM_SPEAKERS);
            } break;

            case SEARCH_TOPICS:
            {
                clearScreen();

                cout << "\n\tSPEAKER'S CORNER - TOPIC SEARCH\n\n";
                searchTopic(speaker, NUM_SPEAKERS);
            } break;

            case DISPLAY_SPEAKER_INFO:
            {
                clearScreen();

                cout << "\n\tSPEAKER'S CORNER - DISPLAY SPEAKER INFORMATION\n\n";
                displaySpeakerInfo(speaker, NUM_SPEAKERS);
            } break;

            case QUIT:
            {
                cout << "\n\tNow exiting 'Speaker's Corner' ...\n"
                          "\tHave a nice day!\n\n";
            } break;
        }
    } while (selection != QUIT);
}

/* **********************************************************
   Definition: getSpeakerID

    This function accepts an array of structure as argument.
    It displays a list of the speaker names. The user is asked
    to enter the speaker's ID, to determine whose info he or
    she wishes to change. The speaker's ID is returend.
   ********************************************************** */

int getSpeakerID(SpeakerInfo speaker[], const int NUM_SPEAKERS)
{
    int index = 0;
    int speakerID = 0;

    for (int index = 0; index < NUM_SPEAKERS; index++)
    {
        cout << "\tSpeaker # " << (index + 1) << "\t"
              << speaker[index].name << "\n";
    }

    cout << "\n\tEnter speaker ID: ";
    cin >> speakerID;

    cout << "\n\n";

    return speakerID - 1;
}

/* **********************************************************
   Definition: getSpeakerInfo

    This function asks the user to enter information about ten
    speakers. This information is stored in the appropriate
    members in the array of structures.
   ********************************************************** */

void getSpeakerInfo(SpeakerInfo speaker[], int mode, const int NUM_SPEAKERS)
{
    int index = 0;
    int numSpeakers = 0;

    if (mode == CHANGE_INFO)
    {
        index = getSpeakerID(speaker, NUM_SPEAKERS);
        numSpeakers = 1;
    }
    else
    {
        index = 0;
        numSpeakers = NUM_SPEAKERS;
    }

    do
    {
        cout << setw(23) << left << "\tSpeaker's Name: ";
        cin.ignore();
        getline(cin, speaker[index].name);

        while (speaker[index].name.empty())
        {
            cout << setw(23) << left << "\tSpeaker's Name: ";
            getline(cin, speaker[index].name);
        }

        cout << setw(22) << left << "\tSpeaker's Telephone #";
        getline(cin, speaker[index].phoneNum);

        while (speaker[index].phoneNum.empty())
        {
            cout << setw(22) << left << "\tSpeaker's Telephone #";
            getline(cin, speaker[index].phoneNum);
        }

        cout << "\tSpeaking Topic:\n\t";
        getline(cin, speaker[index].speakingTopic);

        while (speaker[index].speakingTopic.empty())
        {
            cout << setw(22) << left << "\tSpeaking Topic: ";
            getline(cin, speaker[index].speakingTopic);
        }

        cout << setw(23) << left << "\n\tRequired Fee $";
        cin >> speaker[index].fee;

        while (speaker[index].fee <= 0.0)
        {
            cout << setw(23) << left << "\tRequired Fee $";
            cin >> speaker[index].fee;
        }
        cout << "\n";

        index += 1;
    } while (index < numSpeakers);
}


/* **********************************************************
    Definition: searchTopic

    This function accpets an array of structures as argument.
    It asks the user to enter a key word to search for. If the
    key word is found, the matching speaker information is
    displayed. If there is no match, a message is displayed,
    and the function exits.
    ********************************************************** */

void searchTopic(SpeakerInfo speaker[], const int NUM_SPEAKERS)
{
    int     index = 0;
    int     speakerID = 0;
    string search = " ";
    bool     found = false;

    cout << "\n\tEnter a key word to search for: ";
    cin.ignore();
    getline(cin, search);

    for (index = 0; index < NUM_SPEAKERS; index++)
    {
        if (speaker[index].speakingTopic.find(search) != string::npos)
        {
            found = true;

            if (found)
            {
                cout << fixed << showpoint << setprecision(2);
                cout << setw(22) << left << "\n\tSpeaker's Name: "
                    << speaker[index].name
                    << setw(22) << left << "\n\tSpeaker's Telephone #"
                    << speaker[index].phoneNum
                    << setw(22) << left << "\n\tSpeaking Topic: "
                    << speaker[index].speakingTopic
                    << setw(22) << left << "\n\tRequired Fee: $"
                    << speaker[index].fee << "\n";
                cout << "\n\n";
            }
        }
    }

    if (!found)
    {
            cout << "\n\tNo topic matching this key word.\n\n";
    }

}

/* **********************************************************
   Definition: displaySpeakerInfo

    This function displays information about all speakers in
    the array of structures.
   ********************************************************** */

void displaySpeakerInfo(SpeakerInfo speaker[], const int NUM_SPEAKERS)
{

    for (int index = 0; index < NUM_SPEAKERS; index++)
    {
        cout << fixed << showpoint << setprecision(2);
        cout << setw(22) << left << "\n\tSpeaker's Name: "
              << speaker[index].name
              << setw(22) << left << "\n\tSpeaker's Telephone #"
              << speaker[index].phoneNum
              << setw(22) << left << "\n\tSpeaking Topic: "
              << speaker[index].speakingTopic
              << setw(22) << left << "\n\tRequired Fee: $"
              << speaker[index].fee << "\n";
    }   
    cout << "\n\n";
}

Example Output: 




No comments:

Post a Comment