Wednesday, May 10, 2017

Programming Challenge 10.18 - Phone Number List

Example File: phoneNumbers.txt
/* Phone Number List - This program has an array of at least 10 string objects that
   hold people's names and phone numbers. The following strings are used:
  
    "Alejandra Cruz, 555-1223"        "Joe Looney, 555-0097"      
    "Geri Palmer, 555-8787"            "Li Chen, 555-1212"              
    "Holly Gaddis, 555-8878"        "Sam Wiggins, 555-0998"
    "Bob Kain, 555-8712"                "Tim Haynes, 555-7676"      
    "Warren Gaddis, 555-4939"        "Jean James, 555-4939"          
    "Ron Palmer, 555-2783"

    The program asks 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 are
    displayed. For example, if the user enters "Palmer" the program displays
    the following names from the list:

        "Geri Palmer, 555-8787"            "Ron Palmer, 555-2783" */

#include "Utility.h"

/* Asks the user if he or she wishes to perform another search,
    returns the answer */
char tryAgain();

/* Reads in a list of names and phone-numbers from a file,
   stores the numbers in a string array */
int getDirectory(string *);

/* Searches for a name input by the user, stores the search result,
   returns the result */
string searchDirectory(const string *, const string);

int main()
{
    const int MAX_ENTRIES = 150;
    int        fOpenErr = 0;
    char         again = ' ';
    string   *phoneDir = new string[MAX_ENTRIES];
    string    entry;
    string    name;

    cout << "\n\tPERSONAL PHONE DIRECTORY BUTLER\n\n";

    fOpenErr = getDirectory(phoneDir);
  
    if (fOpenErr != -1)
    {
        do
        {
            cout << "\n\tPlease enter a partial or full name to search for: ";
            getline(cin, name);

            entry = searchDirectory(phoneDir, name);

            cout << "\n\n\tName" << "\t\t\tPhone Number\n\t"
                  << "------------------------------------"
                  << entry
                  << "\n\t------------------------------------\n";

            again = tryAgain();

            if (again == 'N')
            {
                cout << "\n\tHave a nice day!\n";
            }

        } while (again != 'N');
    }

    delete[] phoneDir;
    phoneDir = nullptr;

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: tryAgain

    This function asks the user if he or she wishes to try
    again. This decision is returned.
   ********************************************************** */

char tryAgain()
{
    char again = ' ';

    cout << "\n\tDo you wish to perform another search (y/N)? ";
    cin >> again;
    cin.ignore();

    /* Input validation */
    while (toupper(again) != 'Y' && toupper(again) != 'N')
    {
        cout << "\n\tDo you wish to perform another search(y/N)? ";
        cin >> again;
        cin.ignore();
    }

    return toupper(again);
}

/* **********************************************************
   Definition: getDirectory

    This function reads in a file called 'phoneNumbers.txt'.
    The contents of this file is stored in a string array.
    If the file cannot be openend, the user is informed by a
    message, and the function will exit with code -1.
   ********************************************************** */

int getDirectory(string *nameList)
{
    string   temp;
    ifstream phoneDir;

    phoneDir.open("phoneNumbers.txt");

    if (phoneDir && !phoneDir.eof())
    {
        for (size_t idx = 0; !phoneDir.eof(); idx++)
        {
            getline(phoneDir, temp);

            nameList[idx].append("\n\t" + temp);
        }
    }
    else
    {
        cout << "\n\tFile open error: The file 'phoneNumbers.txt' could not be\n"
              << "\topened or processed. Make sure that the filename is\n"
              << "\tcorrect and the file is not damaged or has been moved\n"
              << "\tfrom the program folder.\n\n"
              << "\tPress enter to exit this program ...";
        return -1;
    }

    phoneDir.close();

    return 0;
}

/* **********************************************************
   Definition: searchDirectory

    This function searches for a name input by the user. If
    a match is found, the result is stored in a string object.
    If there is no match, a message is stored in the string
    object, informing the user accordingly. The search result
    is returned.
   ********************************************************** */

string searchDirectory(const string *phoneDir, const string name)
{
    string result;

    for (size_t idx = 0; idx < phoneDir[idx].length(); idx++)
    {
        if (phoneDir[idx].find(name) != string::npos)
        {
            result.append(phoneDir[idx]);
        }
    }

    if (result.empty())
    {
        result.append("\n\tNo match was found in your directory!");
    }

    return result;
}

Example Output:




No comments:

Post a Comment