Thursday, February 16, 2017

Programming Challenge 7.17 - Name Search

Example Files: GirlNames.txt
                         BoyNames.txt

/* Name Search - This program reads the contents of the following files
   into two seperate arrays or vectors.

      * GirlNames.txt - This file contains a list of the 200 most popular
        names given to girls born in the United States from 2000 to 2009.

      * BoyNames.txt - This file contains a list of the 200 most popular
        names given to boys born in the United States from 2000 to 2009.

   The user is able to enter a boy's name, a girl's name, or both, and
   the application displays messages indicating whether the names were
   among the most popular. */

#include "Utility.h"

/* Prototypes: Menu, Intro, Get girl names, Get boy Names, Compare girl
               names, Compare boy names */
void menu();
void intro();
int getGirlNames(vector<string> &, vector<int> &);
int getBoyNames(vector<string> &, vector<int> &);
void compareGirlNames(vector<string>, vector<int>);
void compareBoyNames(vector<string>, vector<int>);

int main()
{
   menu();

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: Menu

   This function displays a menu which contains the following
   items:

      * Girl Name
      * Boy Name
      * Girl/Boy Name
      * Search (by popularity)
      * Quit

   ********************************************************** */

void menu()
{
   /* Constants: Girl name, Boy name, Girl/Boy name, Quit */
   const int INTRO = 1,
             GIRL_NAME = 2,
             BOY_NAME = 3,
             GIRL_BOY = 4,
             QUIT = 5;

   /* Variable: Select menu item */
   int selectItem = 0;

   /* Vector variables: Girl names, Boy names, Girl popularity
                        index, Boy popularity index */
   vector<string> girlNames, boyNames;
   vector<int> girlPopIndex, boyPopIndex;

   do
   {
      cout << "\t\tPOPULAR GIRL/BOY NAME SEARCH\n\n"
           << "1. INTRODUCTION\n"
           << "2. GIRL NAME\n"
           << "3. BOY NAME\n"
           << "4. GIRL & BOY NAME\n"
           << "5. QUIT\n\n"
           << "Your choice: ";
      cin >> selectItem;
      cout << endl;

      while (selectItem < 1 || selectItem > 5)
      {
         menu();
      }

      switch (selectItem)
      {
         case 1:
            intro();
         break;

         case 2:
            getGirlNames(girlNames, girlPopIndex);       
            girlNames.clear();
         break;

         case 3:
            getBoyNames(boyNames, boyPopIndex);
            boyNames.clear();
        break;

         case 4:
            getGirlNames(girlNames, girlPopIndex);
            getBoyNames(boyNames, boyPopIndex);
            girlNames.clear();
            boyNames.clear();
        break;

         case 5:
            cout << "Good bye!";
         break;
      }

   } while (selectItem != QUIT);
}

/* **********************************************************
   Definition: intro

   This function displays a general introduction to this
   program.
   ********************************************************** */

void intro()
{
   cout << "\t\tPOPULAR GIRL/BOY NAME SEARCH\n\n"
        << "This program allows you to enter a girl name,\n"
        << "a boy name, or a boy and a girl name, to see\n"
        << "how popular they are. If the names are among\n"
        << "the 200 most popular names, a special message\n"
        << "is displayed. Simply choose an item from the\n"
        << "menu, and try it out!\n\n";
}

/* **********************************************************
   Definition: getGirlNames

   This function processes the file "GirlNames.txt". If the
   file is opened successfully, the content is read into the
   vectors:
     
      * gNames - Stores the names
      * gPopCount - Stores the popularity count
   ********************************************************** */

int getGirlNames(vector<string> &gNames, vector<int> &gPopCount)
{
   /* Create file stream object */
   ifstream girlNames;

   /* Variables: Girl names, Popularity counter */
   string gNamesTmp = " ";
   int gCntTmp = 0;

   girlNames.open("GirlNames.txt");

   if (girlNames)
   {
      while (girlNames >> gNamesTmp >> gCntTmp && !girlNames.eof())
      {
         gNames.push_back(gNamesTmp);
         gPopCount.push_back(gCntTmp);
      }

      /* Call: compareBoyNames if the file was processed successfully */
      compareGirlNames(gNames, gPopCount);
   }
   else
   {
      cout << "\nFile open error: The file 'GirlNames.txt' could not\n"
           << "be opened or processed. Please make sure that the filename is\n"
           << "correct and the file is not damaged or has been moved from the\n"
           << "program folder. Please choose 5 to quit this program ...\n\n";

      return 1;
   }
   /* Close file: "GirlNames.txt" */
   girlNames.close();

   return 0;
}

/* **********************************************************
   Definition: getBoyNames

   This function processes the file "BoyNames.txt". If the
   file is opened successfully, the content is read into the
   vectors:
     
      * bNames - Stores the names
      * bPopCount - Stores the popularity count
   ********************************************************** */

int getBoyNames(vector<string> &bNames, vector<int> &bPopCount)
{
   /* Create file stream object */
   ifstream boyNames;

   /* Variables: Boy names, Popularity counter */
   string bNamesTmp = " ";
   int bCntTmp = 0;

   boyNames.open("BoyNames.txt");

   if (boyNames)
   {
      while (boyNames >> bNamesTmp >> bCntTmp && !boyNames.eof())
      {
         bNames.push_back(bNamesTmp);
         bPopCount.push_back(bCntTmp);
      }

      /* Call: compareBoyNames if the file was processed successfully */
      compareBoyNames(bNames, bPopCount);
   }
   else
   {
      cout << "\nFile open error: The file 'BoyNames.txt' could not\n"
         << "be opened or processed. Please make sure that the filename is\n"
         << "correct and the file is not damaged or has been moved from the\n"
         << "program folder. Please choose 5 to quit this program ...\n\n";

      return 1;
   }
   boyNames.close();

   return 0;
}

/* **********************************************************
   Definition: compareGirlNames

   This function accepts the following vectors as arguments:
     
      * girlNames
      * girlPop

   It allows the user to enter a girl name. Once entered, the
   name is compared to the content of the vector, and if it
   is found, the name along with its rank is displayed. If it
   is among the 200 most popular, an additional message is
   displayed.
   ********************************************************** */

void compareGirlNames(vector<string> girlNames, vector<int> girlPop)
{
   /* Variables: Name, Count (loop counter), Error count (accumulator
                 that increments if a name is not found) */
   string name = " ";
   unsigned int count = 0;
   int errCnt = 0;

   /* Ask for a girl name */
   cout << "\nEnter a girl name: ";
   cin >> name;

   /* If the first character in the string that is entered is lowercase,
      it is converted to uppercase */
   if (islower(name.at(0)))
   {
      name[0] = toupper(name[0]);
   }

   /* This loop looks for the existence of a name in the file. If it
      exists, it is displayed. And if it is among the most popular,
      a special message is displayed in addition to that. Else if it
      is in the database, only the name and rank is displayed. If the
      name does not exist, errCnt will increment up to the size of
      girlNames - 1, 999 items. */
   for (count = 0; count < girlNames.size(); count++)
   {
      if (girlNames[count] == name && girlPop[count] <= 300)
      {
         cout << "\n" << girlNames[count]
              << " is among the most popular girl names at place "
              << girlPop[count] << "\n\n";
      }
      else if (girlNames[count] == name)
      {
         cout << "\n" << girlNames[count] << " " << girlPop[count] << "\n\n";
      }     
      else
      {
         errCnt += 1;
      }
   }

   /* If the name was not found, this message is displayed. */
   if (errCnt > 998)
   {
      cout << "\nThe name is not in the database.\n\n";
   }
}

/* **********************************************************
   Definition: compareBoyNames

   This function accepts the following vectors as arguments:
     
      * boyNames
      * boyPop

   It allows the user to enter a boy name. Once entered, the
   name is compared to the content of the vector, and if it
   is found, the name along with its rank is displayed. If it
   is among the 200 most popular, an additional message is
   displayed.
   ********************************************************** */

void compareBoyNames(vector<string> boyNames, vector<int> boyPop)
{
   /* Variables: Name, Count (loop counter), Error count (accumulator
                 that increments if a name is not found) */
   string name = " "; 
   int errCnt = 0;
   unsigned int count = 0;

   /* Get a boy name */
   cout << "Enter a boy name: ";
   cin >> name;

   /* If the first character in the string that is entered is lowercase,
   it is converted to uppercase */
   if (islower(name.at(0)))
   {
      name[0] = toupper(name[0]);
   }

   /* This loop looks for the existence of a name in the file. If it
   exists, it is displayed. And if it is among the most popular,
   a special message is displayed in addition to that. Else if it
   is in the database, only the name and rank is displayed. If the
   name does not exist, errCnt will increment up to the size of
   girlNames - 1, 999 items. */
   for (count = 0; count < boyNames.size(); count++)
   {
      if (boyNames[count] == name && boyPop[count] < 300)
      {
         cout << "\n" << boyNames[count]
              << " is among the most popular boy names at place "
              << boyPop[count] << "\n\n";
      }
      else if (boyNames[count] == name)
      {
         cout << "\n" << boyNames[count] << " " << boyPop[count] << "\n\n";
      }     
      else
      {
         errCnt += 1;
      }
   }

   if (errCnt > 998)
   {
      cout << "\nThe name is not in the database.\n\n";
   }
}

Example Output:









No comments:

Post a Comment