Hi all,
I'm doing a program which Ill post the criteria below. I need to get it to say how many world series they won but it keeps coming back with 0 for everyone. Any help appreciate!
Overview
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:
http://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.
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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Function that builds the array
int build_array( string team_array[] )
{
int cnt = -1;
string teamName;
//Opening file in reading mode
fstream fin("WorldSeriesWinners.txt", ios::in);
//Loop till data exists
while(fin.good())
{
//Fetching a team name
getline(fin, teamName);
//Excluding Not Played data
if(strcmp(teamName.c_str(), "Not Played") != 0)
{
//Incrementing count
cnt++;
//Storing in array
team_array[cnt] = teamName;
}
}
//Closing file
fin.close();
return cnt;
}
//Function that find number of wins
int numWins( string team_array[], int numTeams, string search_team )
{
int i, cnt = 0;
//Loop over teams
for(i=0; i<numTeams; i++)
{
//Comparing team names
if(strcmp((team_array[i]).c_str(), search_team.c_str()) == 0)
cnt++;
}
//Returning count
return cnt;
}
//Main function
int main()
{
const int maxTeams = 120;
string teamArray[maxTeams];
string teamName;
int wins, numTeamsRead;
//Building array
numTeamsRead = build_array(teamArray);
//Reading team name
cout << "\n\n Enter a team: ";
getline(cin, teamName);
//Finding number of wins
wins = numWins(teamArray, numTeamsRead, teamName);
//Printing result
cout << "\n\n The " << teamName << " have won the World Series " << wins << " time(s) \n";
return 0;
}
|