Wednesday, February 15, 2017

Programming Challenge 7.16 - World Series Champions

Example Files: Teams.txt
                         WorldSeriesWinners.txt

/* World Series Champions - This program uses two files:

   * Teams.txt - This file contains a list of several Major League
     baseball teams in alphabetical order. Each team listed in the
     file has won the World Series at least once.

   * WorldSeriesWinners.txt - This file contains a chronological
     list of the World Series' winning teams from 1903 to 2012.
     (The first line in the file is the name of the team that won
     in 1903, and the last line is the name of the team that won in
     2012. The World Series was not played in 1904 and 1994.)

   This program displays the contents of the Teams.txt file on the
   screen and prompts the user to enter the name of one of the teams.
   The program then displays the number of times that team has won
   the World Series in the time period from 1903 to 2012.

     * Tip: Read the contents of the WorldSeriesWinners.txt file into
       an array or vector. When the user enters the name of a team,
       the program should step through the array or vector counting
       the number of times the selected team appears. */

#include "Utility.h"

/* Function prototypes: Display menu, Display intro, Process menu,
   Get teams, Get winners, Display number of wins */
void displayMenu();
void displayIntro();
void processMenu();
void getTeams(vector<string> &);
void getWinners(vector<string> &);
void displayNumWins(const vector<string>, const vector<string>);

int main()
{
   /* Call: displayMenu, processMenu */
   displayMenu();
   processMenu();

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: displayMenu

   This function displays a simple menu for the user to
   choose from.
   ********************************************************** */
void displayMenu()
{
   cout << "\t\tMLB - WORLD SERIES CHAMPION DATABASE MAIN MENU\n\n"
      << "1. INTRODUCTION\n"
      << "2. TEAM ROSTER\n"
      << "3. QUIT\n\n"
      << "Your choice: ";
}

/* **********************************************************
   Definition: processMenu

   This function processes the menu selection.
   ********************************************************** */

void processMenu()
{
   /* Constant: View introduction, Team roster, Quit */
   const int INTRO = 1,
             ROSTER = 2,
             QUIT = 3;

   /* Variable: Menu choice */
   int choice = 0;

   /* Vector variables: Teams, Winners, Number of wins */
   vector<string> teams = { " " };
   vector<string> winners = { " " };

   do
   {
      cin >> (choice);

      /* Call: displayIntro, displayMenu, getTeams,
      getWinners, displayNumWins */
      switch (choice)
      {
         case 1:
         displayIntro();
         displayMenu();
         break;

         case 2:

         /* Clear is called to clear vector winners and teams to
            prevent filling them up with the same content again and
            again after every call of the functions. */
         winners.clear();
         teams.clear();

         getTeams(teams);
         getWinners(winners);
         displayNumWins(winners, teams);

         displayMenu();
         break;

         case 3:
         if (choice == QUIT)
         {
            cout << "Thanks for using this program!\n"
                 << "(Now exiting ...)";
         }
         break;
      }
   } while (choice != QUIT);
}

/* **********************************************************
   Definition: displayIntro

   This function introduces the user to the program.
   ********************************************************** */

void displayIntro()
{
   cout << "\n\t\tMLB - WORLD SERIES CHAMPION DATABASE\n\n"
      << "\tThis database contains a number of teams, who, at\n"
      << "\tleast once in the period between 1903 and 2012, won\n"
      << "\tthe Major League Baseball World Series. If you wish to\n"
      << "\tsee how often a particular team has won, you simply have\n"
      << "\tto select: 'TEAM ROSTER' from the menu, enter a name that\n"
      << "\tis displayed in the list, and the result will be displayed\n"
      << "\timmediately.\n\n";
}

/* **********************************************************
   Definition: getTeams

   This function reads in the file "Teams.txt" and stores
   its content in the vector teams.
   ********************************************************** */

void getTeams(vector<string> &teams)
{
   /* Clear is called to clear vector teams to prevent filling it up
      with the same content again and again, every time this function
      enters after the first run. */
   teams.clear();

   /* Create file stream objects: Teams (for "Teams.txt") */
   ifstream teamsFile;

   /* Variable: Team roster (for holding the contents
   of "Teams.txt")*/
   string teamRoster = " ";

   /* Open the files: "Teams.txt", "WorldSeriesWinners.txt" */
   teamsFile.open("Teams.txt");

   /* If the file was opened successfully, and the end of file is
   not reached, the contents of "Teams.txt" is read into teams
   with getline, then processed and stored in the vector teams */
   if (teamsFile)
   {
      while (getline(teamsFile, teamRoster) && !teamsFile.eof())
      { 
         teams.push_back(teamRoster);
      }     
   }
   else
   {
      cout << "\nFile open error: The file 'Teams.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.\n\n"
         << "This program will now exit ...";
   }

   /* Close file: "Teams.txt" */
   teamsFile.close();
}

/* **********************************************************
   Definition: getWinners

   This function reads in the file "WorldSeriesWinners.txt",
   and stores its content in the vector teams.
   ********************************************************** */

void getWinners(vector<string> &winners)
{
   /* Create file stream objects: winnersFile
   (for "WorldSeriesWinners.txt) */
   ifstream winnersFile;

   /* Variable: Winning roster (for holding the contents of
   "WorldSeriesWinners.txt") */
   string winningRoster = " ";

   /* Open file: "WorldSeriesWinners.txt" */
   winnersFile.open("WorldSeriesWinners.txt");

   /* If the file was opened successfully, and the end of file is
   not reached, the contents of "WorldSeriesWinners.txt" is
   read into winningRoster with getline, then processed and
   stored in the vector winners */
   if (winnersFile)
   {
      while (getline(winnersFile, winningRoster) && !winnersFile.eof())
      {
         winners.push_back(winningRoster);
      }
   }
   else
   {
      cout << "\nFile open error: The file 'WorldSeriesWinners.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.\n\n"
         << "This program will now exit ...";
   }

   /* Close file: "WorldSeriesWinners.txt" */
   winnersFile.close();
}

/* **********************************************************
   Definition: getNumWins

   This function accepts the following vectors:

      * teams<>
      * winners<>

   It asks the user for the team name, then it determines,
   how often a team has won, and displays the result.
   ********************************************************** */

void displayNumWins(const vector<string> winners, const vector<string> teams)
{

   /* Variables: Team name, Count (accumulator to count the number of
      times a team is listed in vector winners) */
   string teamName = " ";
   int cnt = 0;

   /* Display: The team roster */
   cout << "\nTeam Roster:\n\n";
   for (string v : teams)
   {
      cout << v << "\n";
   }
  
   /* Get a team name from the user */
   cout << "\nEnter a team name: ";
   cin.get();
   getline(cin, teamName);

   /* This loop checks to see whehter the teamName matches the input
      and then counts the occurence in the vector winners, which is
      then stored in cnt */
   for (size_t n = 0; n < winners.size(); n++)
   {  
      if (teamName == winners[n])
      {
         cnt++;
      }
   }

   /* Display the number of wins of any given team */
   cout << "\nThe " << teamName << " have won the Major League Baseball "
      << "World Series " << cnt << " times!\n\n";
}


Example Output:

 







No comments:

Post a Comment