Question

****PLEASE CODE IN C++ and line doc explaining what is happening!***** **HERE IS THE INPUT FILE(.txt)*****...

****PLEASE CODE IN C++ and line doc explaining what is happening!*****
**HERE IS THE INPUT FILE(.txt)*****
Ottawa Senators
New York Rangers
Boston Bruins
Montreal Canadiens
Montreal Canadiens
Toronto Maple Leafs
New York Rangers
Chicago Blackhawks
Montreal Maroons
Detroit Red Wings
Detroit Red Wings
Chicago Blackhawks
Boston Bruins
New York Rangers
Boston Bruins
Toronto Maple Leafs
Detroit Red Wings
Montreal Canadiens
Toronto Maple Leafs
Montreal Canadiens
Toronto Maple Leafs
Toronto Maple Leafs
Toronto Maple Leafs
Detroit Red Wings
Toronto Maple Leafs
Detroit Red Wings
Montreal Canadiens
Detroit Red Wings
Detroit Red Wings
Montreal Canadiens
Montreal Canadiens
Montreal Canadiens
Montreal Canadiens
Montreal Canadiens
Chicago Blackhawks
Toronto Maple Leafs
Toronto Maple Leafs
Toronto Maple Leafs
Montreal Canadiens
Montreal Canadiens
Toronto Maple Leafs
Montreal Canadiens
Montreal Canadiens
Boston Bruins
Montreal Canadiens
Boston Bruins
Montreal Canadiens
Philadelphia Flyers
Philadelphia Flyers
Montreal Canadiens
Montreal Canadiens
Montreal Canadiens
Montreal Canadiens
New York Islanders
New York Islanders
New York Islanders
New York Islanders
Edmonton Oilers
Edmonton Oilers
Montreal Canadiens
Edmonton Oilers
Edmonton Oilers
Calgary Flames
Edmonton Oilers
Pittsburgh Penguins
Pittsburgh Penguins
Montreal Canadiens
New York Rangers
New Jersey Devils
Colorado Avalanche
Detroit Red Wings
Detroit Red Wings
Dallas Stars
New Jersey Devils
Colorado Avalanche
Detroit Red Wings
New Jersey Devils
Tampa Bay Lightning
Not Awarded
Carolina Hurricanes
Anaheim Ducks
Detroit Red Wings
Pittsburgh Penguins
Chicago Blackhawks
Boston Bruins
Los Angeles Kings
Chicago Blackhawks
Los Angeles Kings
Chicago Blackhawks
Pittsburgh Penguins
Pittsburgh Penguins
Washington Capitals

Overview

The Stanley Cup is the trophy awarded annually to the playoff champion of the National Hockey League (NHL). It is the oldest professional sports trophy in North America.
For this assignment, write a program that will process a file of data that contains the winners of the Stanley Cup between 1927 and 2018. It will allow the user of the program to name a hockey team and learn how many times that the team has won the Stanley Cup.

Processing
The main function for this program is pretty basic. It should start by creating an array that can hold a maximum of 100 string elements. Each element in the array will be the name of a team that won the Stanley Cup (or "Not Awarded"). There should also be an integer to hold the number of teams that are in the array, an integer to hold the number of times a specific team has won the Stanley Cup, and a string to hold a specific team name.
Call the buildArray function that is described below to fill the array with the names of the teams. The value returned from the function should be saved in the integer that was created to hold the number of teams that are in the array.
Display the integer value that is returned from the buildArray function with an appropriate label.
Prompt the user for the name of a hockey team and save the value in the string variable.
Call the numWins function that is described below to find the number of times the selected team has won the Stanley Cup. The value returned from the function should be saved in the integer that was created to hold the number of times that a specific team has won the Stanley Cup.
Finally, display either the number of times that the selected team has won the Stanley Cup or the number of times that the Cup was not awarded if the user entered "Not Awarded".
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 Blackhawks
New York Rangers
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 Stanley Cup. 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 Stanley Cup winning teams to determine the number of times that a specific team has won the Stanley Cup.
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 100.
Programming Requirements:
1. Add #include <cstdlib> and #include <fstream> at the top of the program. Depending on the compiler, #include <string> might also have to be added at the top of the program to be able to use the getline function.
Output
The output will depend on the teams that the user specifies.
Run 1
There are 92 entries in the input file

Team? Chicago Blackhawks

The Chicago Blackhawks have won the Stanley Cup 6 time(s).
Run 2
There are 92 entries in the input file

Team? Montreal Canadiens

The Montreal Canadiens have won the Stanley Cup 22 time(s).
Run 3
There are 92 entries in the input file

Team? Not Awarded

The Stanley Cup was not awarded 1 time(s).
Run 4
There are 92 entries in the input file

Team? Nashville Predators

The Nashville Predators have won the Stanley Cup 0 time(s).
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Program:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//Constants
const int SIZE = 100;

//Function that builds the array using file
int buildArray( string team_array[] )
{
    int idx=0;

    //Opening file for reading
    fstream fin("stanley.txt", ios::in);

    //Iterating over file
    while(fin.good())
    {
        //Fetching a line
        getline(fin, team_array[idx]);
        idx++;
    }

    //Closing file
    fin.close();

    return idx;
}

//Function that returns the number of wins
int numWins( string team_array[], int numTeams, string search_team )
{
    int i, cnt=0;

    //Iterating over array
    for(i=0; i<numTeams; i++)
    {
        //Comparing elements
        if(team_array[i] == search_team)
            //Updating count
            cnt++;
    }

    return cnt;
}

//Main function
int main()
{
    //Array that holds the names
    string teams[SIZE];
    int numTeams;

    string teamName;
    int timesWon;

    //Calling functions
    numTeams = buildArray(teams);

    cout << "\n There are " << numTeams << " entries in the input file \n";

    //Reading team name
    cout << "\n Team? ";
    getline(cin, teamName);

    //Finding number of wins
    timesWon = numWins(teams, numTeams, teamName);

    //Checking Not Awarded
    if(teamName == "Not Awarded")
    {
        cout << "\n The Stanley Cup was not awarded " << timesWon << " time(s). \n";
    }
    else
    {
        cout << "\n The " << teamName << " have won the Stanley Cup " << timesWon << " time(s). \n";
    }

    return 0;
}


________________________________________________________________________________

Sample Run:

CTCStanleyCuplbin Debug StanleyCup.exe There are 92 entries in the input file Team? Nashville Predators The Nashville Predators have won the Stanley Cup e time(s) Process returned 0 (0x0) execution time : 4.308 s Press any key to continue.

Add a comment
Know the answer?
Add Answer to:
****PLEASE CODE IN C++ and line doc explaining what is happening!***** **HERE IS THE INPUT FILE(.txt)*****...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java : I keep getting something wrong in my loadfile method, how do I fix my...

    Java : I keep getting something wrong in my loadfile method, how do I fix my code? Thank you. here is what is contained in the text file (WorldSeriesWinners.txt) 112 1903 Boston Americans 1904 No World Series 1905 New York Giants 1906 Chicago White Sox 1907 Chicago Cubs 1908 Chicago Cubs 1909 Pittsburgh Pirates 1910 Philadelphia Athletics 1911 Philadelphia Athletics 1912 Boston Red Sox 1913 Philadelphia Athletics 1914 Boston Braves 1915 Boston Red Sox 1916 Boston Red Sox 1917 Chicago...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT