Description
For this assignment, write a program that will process a file of data about winners of the World Series between 1903 and 2015. It will allow the user of the program to name a baseball team and learn how many times that the team has won the World Series.
Processing
The main function for this program is pretty basic. It should start by creating an array that can hold a maximum of 120 string elements. Each element in the array will be the name of a team that won the world series.
Call the buildArray function that is described below to fill the array with the names of the teams.
Prompt the user for the name of a baseball team and save the value in a string variable.
Call the numWins function that is described below to find the number of times the selected team has won the World Series.
Finally, display the number of times that the selected team has won the World Series.
Input File
The file with the input data can be downloaded and saved on your computer. It is called WorldSeriesWinners.txt and can be found here:
https://faculty.cs.niu.edu/~byrnes/csci240/pgms/WorldSeriesWinners.txt
The file contains the names of the teams that have won the World Series in the order that they were won from 1903 through 2015. The World Series was not played in the years 1904 and 1994. Therefore, those two years have the string “Not Played” rather than a team name.
Reading a string
In the previous assignments, whenever an input operation was performed, the input operator (>>) was used with either cin or an input file stream variable. This worked because the information that was being inputted was either separated by whitespace (a space or a newline character) or was being read one character at a time.
For this assignment, the input operator CANNOT be used because the team names will have at least one space separating the parts of the name. For example:
Chicago Cubs
New York Yankees
Rather than trying to read the team names in parts, it should be read as a “whole” string. To do this, the getline function must be used. The format for getline is:
getline( name_of_the_input_stream, name_of_the_string_to_hold_data );
So, if the information should come from standard input (the keyboard):
string str; getline( cin, str );
or from an input file:
string str; getline( infile, str );
The Functions
For this program, two functions are required:
int buildArray( string team_array[] )
This function will place the values from the input file into the passed in array of strings.
It takes one argument: an array of strings that will be filled with the names of the teams that have won the World Series. It returns an integer: the number of teams that were placed into the array.
This function should use a standard read loop pattern to read the data from the file and place it into the array. Read the first team name from the file. In a loop that executes while there is data in the file, put the team name that was read into the array, update the array subscript, and get another name from the file.
Once all of the data has been read from the file, return the number of teams that were placed into the array.
int numWins( string team_array[], int numTeams, string search_team )
This function will search the array of World Series winning teams to determine the number of times that a specific team has won the World Series.
It takes three arguments: an array of strings that will be searched, an integer that represents the number of teams that are in the array (ie. the number of teams to be searched), and a string that represents the specific team to search for in the array. It returns an integer: the number of times that the specified team was found in the array.
In a loop that executes exactly one time for the number of teams that are in the array (ie. numTeams number of times), check to see if the specific team to search for in the array (ie. search_team) is found in the array of strings that will be searched (ie. team_array). If the specific team is found, increment a counter by 1.
Once the loop is finished executing, return the counter.
Symbolic Constant
This program requires the use of 1 symbolic constant. The constant should represent the maximum number of teams that can be placed into the array. The value should be 120.
Programming Requirements:
- Depending on the compiler, you might have to add #include <string> at the top of the program to be able to use the getline function.
- Hand in a copy of your source code using Blackboard.
Output
The output will depend on the teams that the user specifies.
Run 1
Enter a team: Chicago Cubs The Chicago Cubs have won the World Series 2 time(s).
Run 2
Enter a team: New York Yankees The New York Yankees have won the World Series 27 time(s).
Run 3
Enter a team: Washington Nationals The Washington Nationals have won the World Series 0 time(s).
Extra Credit
For up to 5 points of extra credit, modify the program to include a display of the years that the user’s specified team won the World Series.
The modification should be made by coding a third function named displayYears, that should be called in main. However, it should only be called if the team has won a World Series.
void displayYears( string team_array[], int numTeams, string search_team )
This function will search the array of World Series winning teams to determine and display the exact years that a specific team has won the World Series.
It takes three arguments: an array of strings that will be searched, an integer that represents the number of teams that are in the array (ie. the number of teams to be searched), and a string that represents the specific team to search for in the array. It returns nothing.
The years should be displayed with a maximum of 7 values per line of output.
Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don’t take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.
Extra Credit Output
Run 1
Enter a team: Chicago Cubs The Chicago Cubs have won the World Series 2 time(s). The World Series was won in: 1907 1908
Run 2
Enter a team: New York Yankees The New York Yankees have won the World Series 27 time(s). The World Series was won in: 1923 1927 1928 1932 1936 1937 1938 1939 1941 1943 1947 1949 1950 1951 1952 1953 1956 1958 1961 1962 1977 1978 1996 1998 1999 2000 2009
Run 3
Enter a team: Washington Nationals The Washington Nationals have won the World Series 0 time(s).
One Final Note
Some teams that have won the World Series have changed their name between 1903 and 2015. For example, the team currently known as the Boston Red Sox were originally known as the Boston Americans until 1907. Our program will not take into account the different names. So the Boston Red Sox should be shown as having won 7 World Series and the Boston Americans as having won 1 World Series.