Word Series Task : Write a C++ program that displays the contents of the Teams.t
ID: 3737089 • Letter: W
Question
Word Series
Task: Write a C++ program that 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 should then display the number of times that team has won the World Series in the time period from 1903 to 2012.
Input: The name of one of the teams displayed from the Teams.txt file.
Processing: Read the contents of the WorldSeriesWinners.txt file into a vector. When the user enters the name of a team, the program should step through the vector counting the number of times the selected team appears.
Output: The number of times the team entered as input has won the world series and the year(s) that they won.
1.) 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.
Anaheim Angels
Arizona Diamondbacks
Atlanta Braves
Baltimore Orioles
Boston Americans
Boston Braves
Boston Red Sox
Brooklyn Dodgers
Chicago Cubs
Chicago White Sox
Cincinnati Reds
Cleveland Indians
Detroit Tigers
Florida Marlins
Kansas City Royals
Los Angeles Dodgers
Milwaukee Braves
Minnesota Twins
New York Giants
New York Mets
New York Yankees
Oakland Athletics
Philadelphia Athletics
Philadelphia Phillies
Pittsburgh Pirates
San Francisco Giants
St. Louis Cardinals
Toronto Blue Jays
Washington Senators
2.) 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. Note that the World Series was not played in 1904 and 1994).
Boston Americans
New York Giants
Chicago White Sox
Chicago Cubs
Chicago Cubs
Pittsburgh Pirates
Philadelphia Athletics
Philadelphia Athletics
Boston Red Sox
Philadelphia Athletics
Boston Braves
Boston Red Sox
Boston Red Sox
Chicago White Sox
Boston Red Sox
Cincinnati Reds
Cleveland Indians
New York Giants
New York Giants
New York Yankees
Washington Senators
Pittsburgh Pirates
St. Louis Cardinals
New York Yankees
New York Yankees
Philadelphia Athletics
Philadelphia Athletics
St. Louis Cardinals
New York Yankees
New York Giants
St. Louis Cardinals
Detroit Tigers
New York Yankees
New York Yankees
New York Yankees
New York Yankees
Cincinnati Reds
New York Yankees
St. Louis Cardinals
New York Yankees
St. Louis Cardinals
Detroit Tigers
St. Louis Cardinals
New York Yankees
Cleveland Indians
New York Yankees
New York Yankees
New York Yankees
New York Yankees
New York Yankees
New York Giants
Brooklyn Dodgers
New York Yankees
Milwaukee Braves
New York Yankees
Los Angeles Dodgers
Pittsburgh Pirates
New York Yankees
New York Yankees
Los Angeles Dodgers
St. Louis Cardinals
Los Angeles Dodgers
Baltimore Orioles
St. Louis Cardinals
Detroit Tigers
New York Mets
Baltimore Orioles
Pittsburgh Pirates
Oakland Athletics
Oakland Athletics
Oakland Athletics
Cincinnati Reds
Cincinnati Reds
New York Yankees
New York Yankees
Pittsburgh Pirates
Philadelphia Phillies
Los Angeles Dodgers
St. Louis Cardinals
Baltimore Orioles
Detroit Tigers
Kansas City Royals
New York Mets
Minnesota Twins
Los Angeles Dodgers
Oakland Athletics
Cincinnati Reds
Minnesota Twins
Toronto Blue Jays
Toronto Blue Jays
Atlanta Braves
New York Yankees
Florida Marlins
New York Yankees
New York Yankees
New York Yankees
Arizona Diamondbacks
Anaheim Angels
Florida Marlins
Boston Red Sox
Chicago White Sox
St. Louis Cardinals
Boston Red Sox
Philadelphia Phillies
New York Yankees
San Francisco Giants
St. Louis Cardinals
San Francisco Giants
Explanation / Answer
#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 << " MLB - WORLD SERIES CHAMPION DATABASE MAIN MENU "
<< "1. INTRODUCTION "
<< "2. TEAM ROSTER "
<< "3. QUIT "
<< "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! "
<< "(Now exiting ...)";
}
break;
}
} while (choice != QUIT);
}
/* **********************************************************
Definition: displayIntro
This function introduces the user to the program.
********************************************************** */
void displayIntro()
{
cout << " MLB - WORLD SERIES CHAMPION DATABASE "
<< " This database contains a number of teams, who, at "
<< " least once in the period between 1903 and 2012, won "
<< " the Major League Baseball World Series. If you wish to "
<< " see how often a particular team has won, you simply have "
<< " to select: 'TEAM ROSTER' from the menu, enter a name that "
<< " is displayed in the list, and the result will be displayed "
<< " immediately. ";
}
/* **********************************************************
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 << " File open error: The file 'Teams.txt' could not "
<< "be opened or processed. Please make sure that the filename is "
<< "correct and the file is not damaged or has been moved from the "
<< "program folder. "
<< "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 << " File open error: The file 'WorldSeriesWinners.txt' could not "
<< "be opened or processed. Please make sure that the filename is "
<< "correct and the file is not damaged or has been moved from the "
<< "program folder. "
<< "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 << " Team Roster: ";
for (string v : teams)
{
cout << v << " ";
}
/* Get a team name from the user */
cout << " Enter 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 << " The " << teamName << " have won the Major League Baseball "
<< "World Series " << cnt << " times! ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.