Using C write a program using structs and the string class

Using C++ , write a program using structs and the string class.

Read a file containing team names, their conferences and divisions. This data will be stored in an array of structs.
Read a file containing scores. Each record in the file must be parsed to determine the winning and losing team. The number of wins, losses and percentage must be calculated using the data in the scores file.
The array of structs must be sorted by conference, division, and percentage to produce output equivalent to that shown below.

Use the input files, team name file (http://voyager.deanza.edu/~bentley/nfl_teams.txt)

and scores file (http://voyager.deanza.edu/~bentley/nfl_scores.txt).

The scores file may be updated again at a later date.

Use this struct to store the data for each team:

struct NFL_Team
{
    string name;
    string conference;
    string division;
    unsigned short wins;
    unsigned short losses;    
  unsigned short ties;
    float pct;
};

Use strings to store all character data for this assignment..

The program output must show the current standings using the team input data and the percentage calculations.

You may not use stringstream (or istringstream or ostringstream) objects or c-string function for this assignment.

Program Output

Output should look quite similar to the following. Note: the actual statistics will be different with the current score live

There are 32 NFL teams, 16 in each conference. The conferences are American and National. There are 8 divisions with 4 teams in each division.

The scores file will also require specific parsing techniques. Skip over the date column. To parse a scores record, use string functions looking for a comma. It will be handy to write a function that can pick out a team name from a text String. The string find() and rfind() functions will be useful here.

To calculate the team percentage, use the formula:
pct = (wins + 0.5 * ties) / total games played

To sort the data for the output standings, you might want to create a \"sortkey\" function, consisting of the conference, division, and percentage. Then you can sort using the \"sortkey\".

And finally, as usual, this assignment contains some subtle aspects. Be sure to start early and allow enough time to ask the instructor for help.

Recommendation, when you start, use a small subset of the scores file to make sure you are correctly calculating the statistics.

Be sure your code handles ties. There may be one/some in the test file used to grade your assignment.

National Football Conference West Division Seattle Seahawks Arizona Cardinals Los Angeles Rams San Francisco 49ers Pct 0 0.800 0 0.500 0 0.500 0 0.167 South Division Atlanta Falcons New Orleans Saints Tampa Bay Buccaneers Carolina Panthers Pct 0 0.667 0 0.400 0 0.400 0 0.167 North Division Minnesota Vikings Green Bay Packers Detroit Lions Chicago Bears Pct 0 1.000 0 0.667 0 0.500 0 0.143 East Division Dallas Cowboys Washington Redskins Philadelphia Eagles New York Giants Pct 0 0.833 0 0.667 0 0.600 0 0.500 American Football Conference West Division Denver Broncos Oakland Raiders Kansas City Chiefs San Diego Chargers Pct 0 0.667 0 0.667 0 0.600 0 0.333 South Division HoustonTexans Tennessee Titans Jacksonville Jaquars Indianapolis Colts Pct 0 0.667 0 0.500 0 0.400 0 0.333 North Division Pittsburgh Steelers Baltimore Ravens Cincinnati Bengals Cleveland Browns Pct 0 0.667 0 0.500 0 0.333 0 0.000 East Division New England Patriots Pct 0 0.833

Solution

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
#include <sstream>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;

struct NFL_Team
{
    string name;
    string conference;
    string division;
    unsigned short wins;
    unsigned short losses;
    unsigned short ties;
    float pct;
};

const int size = 32;


void getTeamNames(const string& filename, NFL_Team* team);
void getScores(const string& filename, NFL_Team* team);
int isInArray(string word, NFL_Team* team);
void sort(NFL_Team* team, int b);

int main()
{
    const string teamfile = \"teamNameFile.txt\";
    const string scorefile = \"scoresFile.txt\";

    NFL_Team team[size];

    for(int i=0;i<size;i++){
        team[i].wins = 0;
        team[i].losses = 0;
        team[i].ties = 0;
    }

    getTeamNames(teamfile, team);
    getScores(scorefile, team);

    for(int i=0;i<size;i++)
    {
    team[i].pct = (team[i].wins+ .5*team[i].ties)/((team[i].wins + team[i].losses));
    }
        int b =0;

        for (int c=1; c>=0;c--)
        {
        cout << endl;
        cout << team[c].conference << endl;

        for (int i = 3; i >= 0; i--)
        {
            cout << endl;
            cout << left << setw(25) << team[i+(c*4)].division;
            cout << left << setw(6) << \"W\";
            cout << left << setw(6) << \"L\";
            cout << left << setw(6) << \"T\";
            cout << right << setw(6) << \"Pct\";
            cout << endl;

            for(int a=0;a<4;a++)
            {
            b = a+i*4+c*16;

            //sort(team,b);

            cout << left << setw(25)<< team[b].name;
            cout << left << setw(6) << team[b].wins;
            cout << left << setw(6) << team[b].losses;
            cout << left << setw(6) << team[b].ties;
            cout << right << fixed << setprecision(3)<< setw(6) << team[b].pct;
            cout << endl;
            b++;
            }
        }
        }


}

void getScores(const string& filename, NFL_Team* team)
{
    ifstream myfile(\"scoresFile.txt\");
    if (myfile)
   {
       while(!myfile.eof()){
       string date;
       string winnerloser;
       myfile >> date;
       getline(myfile,winnerloser);
//       cout << date << endl;
//       cout << winnerloser << endl;

        int a;
        int b;

        string str = winnerloser;
        string word;
        string word2;
        string lastletters;
        string lastletters2;
        stringstream stream(str);
        getline(stream, word, \',\');
        getline(stream, word2, \'\ \');
        lastletters.append(word, word.length()-2, word.length());
        lastletters2.append(word2,word2.length()-2,word2.length());


        a= atoi(lastletters.c_str());
        b= atoi(lastletters2.c_str());
        //cout << a << endl;
        //cout << b << endl;

        string winner;
        string loser;

        word.erase(word.length()-3,word.length());
        word.erase(word.begin());
        word2.erase(word2.length()-2,word2.length());
        word2.erase(word2.begin());
        word2.erase(word2.find_last_not_of(\" \\t\ \ \\f\\v\") + 1);

        int location = 0;

        if (a > b)
        {
            //cout << word << endl;
            location = isInArray(word,team);
//           cout << team[location].name << endl;
            team[location].wins++;

//            cout << word2 << endl;
            location = isInArray(word2,team);
//            cout << team[location].name << endl;
            team[location].losses++;


        }
        else
        {
            location = isInArray(word,team);
            team[location].ties++;
            location = isInArray(word2,team);
            team[location].ties++;

        }
       }
       myfile.close();
   }
   else cout << \"File not found\" << endl;
}

void getTeamNames(const string& filename, NFL_Team* team)
{
    string buffer;

   ifstream myfile(\"teamNameFile.txt\");
   if (myfile)
   {

       int b =0;

        for (int c=0; c<2;c++)
        {

        getline(myfile, team[c].conference);

        for (int i = 0; i < 4; i++)
        {
            getline(myfile,buffer);
            getline(myfile, team[i+(c*4)].division );

            for(int a=0;a<4;a++)
            {
            b = a+i*4+c*16;
            getline(myfile, team[b].name);
            b++;
            }

        }

        getline(myfile,buffer);

        }
        myfile.close();

    }

       else cout << \"File not found\" << endl;
}

int isInArray(string word, NFL_Team* team)
{
    for (int i=0;i<size;i++)
    {
    if (word.compare(team[i].name) == 0)
        return i;
    }
    return 0;
}

void sort(NFL_Team* team, int b)
{
    NFL_Team temp;


        for(int a=0;a<4;a++)
        {

        for(int i = 0; i < b; i++)
        {
            for (int j = i+1 ; j < b; j++)
            {
                if (team[i].pct > team[j].pct)
                {
               temp = team[i];
               team[i] = team[j];
               team[j] = temp;
                }
            }
        }
        }

}


teamNameFile.txt

American Football Conference

AFC East
Buffalo Bills
Miami Dolphins
New England Patriots
New York Jets

AFC North
Baltimore Ravens
Cincinnati Bengals
Cleveland Browns
Pittsburgh Steelers

AFC South
Houston Texans
Indianapolis Colts
Jacksonville Jaguars
Tennessee Titans

AFC West
Denver Broncos
Kansas City Chiefs
Oakland Raiders
San Diego Chargers

National Football Conference

NFC East
Dallas Cowboys
New York Giants
Philadelphia Eagles
Washington Redskins

NFC North
Chicago Bears
Detroit Lions
Green Bay Packers
Minnesota Vikings

NFC South
Atlanta Falcons
Carolina Panthers
New Orleans Saints
Tampa Bay Buccaneers

NFC West
Arizona Cardinals
San Francisco 49ers
Seattle Seahawks
St. Louis Rams

scoresFile.txt

09/10/15 New England Patriots 28, Pittsburgh Steelers 21
09/13/15 San Diego Chargers 33, Detroit Lions 28
09/13/15 Buffalo Bills 27, Indianapolis Colts 14
09/13/15 New York Jets 31, Cleveland Browns 10
09/13/15 Cincinnati Bengals 33, Oakland Raiders 13
09/13/15 Arizona Cardinals 31, New Orleans Saints 19
09/13/15 Carolina Panthers 20, Jacksonville Jaguars 9
09/13/15 Denver Broncos 19, Baltimore Ravens 13
09/13/15 Dallas Cowboys 27, New York Giants 26
09/13/15 St. Louis Rams 34, Seattle Seahawks 31
09/13/15 Kansas City Chiefs 27, Houston Texans 20
09/13/15 Green Bay Packers 31, Chicago Bears 23
09/13/15 Miami Dolphins 17, Washington Redskins 10
09/13/15 Tennessee Titans 42, Tampa Bay Buccaneers 14
09/14/15 San Francisco 49ers 20, Minnesota Vikings 3
09/14/15 Atlanta Falcons 26, Philadelphia Eagles 24
09/17/15 Denver Broncos 31, Kansas City Chiefs 24
09/20/15 Atlanta Falcons 24, New York Giants 20
09/20/15 Tampa Bay Buccaneers 26, New Orleans Saints 19
09/20/15 Washington Redskins 24, St. Louis Rams 10
09/20/15 Carolina Panthers 24, Houston Texans 17
09/20/15 Minnesota Vikings 26, Detroit Lions 16
09/20/15 Cincinnati Bengals 24, San Diego Chargers 19
09/20/15 Cleveland Browns 28, Tennessee Titans 14
09/20/15 Jacksonville Jaguars 23, Miami Dolphins 20
09/20/15 Dallas Cowboys 20, Philadelphia Eagles 10
09/20/15 Pittsburgh Steelers 43, San Francisco 49ers 18
09/20/15 Arizona Cardinals 48, Chicago Bears 23
09/20/15 Green Bay Packers 27, Seattle Seahawks 17
09/20/15 Oakland Raiders 37, Baltimore Ravens 33
09/20/15 New England Patriots 40, Buffalo Bills 32
09/21/15 New York Jets 20, Indianapolis Colts 7
09/24/15 New York Giants 32, Washington Redskins 21
09/27/15 Carolina Panthers 27, New Orleans Saints 22
09/27/15 Philadelphia Eagles 24, New York Jets 17
09/27/15 Pittsburgh Steelers 12, St. Louis Rams 6
09/27/15 Houston Texans 19, Tampa Bay Buccaneers 9
09/27/15 Arizona Cardinals 47, San Francisco 49ers 7
09/27/15 Atlanta Falcons 39, Dallas Cowboys 28
09/27/15 New England Patriots 51, Jacksonville Jaguars 17
09/27/15 Denver Broncos 24, Detroit Lions 12
09/27/15 Cincinnati Bengals 28, Baltimore Ravens 24
09/27/15 Indianapolis Colts 35, Tennessee Titans 33
09/27/15 Seattle Seahawks 26, Chicago Bears 0
09/27/15 Oakland Raiders 27, Cleveland Browns 20
09/27/15 Minnesota Vikings 31, San Diego Chargers 14
09/27/15 Buffalo Bills 41, Miami Dolphins 14
09/28/15 Green Bay Packers 38, Kansas City Chiefs 28
10/01/15 Baltimore Ravens 23, Pittsburgh Steelers 20
10/04/15 New York Giants 24, Buffalo Bills 10
10/04/15 New Orleans Saints 26, Dallas Cowboys 20
10/04/15 Denver Broncos 23, Minnesota Vikings 20
10/04/15 San Diego Chargers 30, Cleveland Browns 27
10/04/15 Cincinnati Bengals 36, Kansas City Chiefs 21
10/04/15 Indianapolis Colts 16, Jacksonville Jaguars 13
10/04/15 Atlanta Falcons 48, Houston Texans 21
10/04/15 New York Jets 27, Miami Dolphins 14
10/04/15 Washington Redskins 23, Philadelphia Eagles 20
10/04/15 St. Louis Rams 24, Arizona Cardinals 22
10/04/15 Green Bay Packers 17, San Francisco 49ers 3
10/04/15 Carolina Panthers 37, Tampa Bay Buccaneers 23
10/04/15 Chicago Bears 22, Oakland Raiders 20
10/05/15 Seattle Seahawks 13, Detroit Lions 10
10/08/15 Indianapolis Colts 27, Houston Texans 20
10/11/15 Cleveland Browns 33, Baltimore Ravens 30
10/11/15 Buffalo Bills 14, Tennessee Titans 13
10/11/15 Cincinnati Bengals 27, Seattle Seahawks 24
10/11/15 Chicago Bears 18, Kansas City Chiefs 17
10/11/15 New York Giants 30, San Francisco 49ers 27
10/11/15 Philadelphia Eagles 39, New Orleans Saints 17
10/11/15 Denver Broncos 16, Oakland Raiders 10
10/11/15 Arizona Cardinals 42, Detroit Lions 17
10/11/15 Atlanta Falcons 25, Washington Redskins 19
10/11/15 New England Patriots 30, Dallas Cowboys 6
10/11/15 Tampa Bay Buccaneers 38, Jacksonville Jaguars 31
10/11/15 Green Bay Packers 24, St. Louis Rams 10
10/12/15 Pittsburgh Steelers 24, San Diego Chargers 20
10/15/15 New Orleans Saints 31, Atlanta Falcons 21
10/18/15 Miami Dolphins 38, Tennessee Titans 10
10/18/15 Carolina Panthers 27, Seattle Seahawks 23
10/18/15 New York Jets 34, Washington Redskins 20
10/18/15 Cincinnati Bengals 34, Buffalo Bills 21
10/18/15 Houston Texans 31, Jacksonville Jaguars 20
10/18/15 San Francisco 49ers 25, Baltimore Ravens 20
10/18/15 Minnesota Vikings 16, Kansas City Chiefs 10
10/18/15 New England Patriots 34, Indianapolis Colts 27
10/18/15 Green Bay Packers 27, San Diego Chargers 20
10/18/15 Denver Broncos 26, Cleveland Browns 23
10/18/15 Detroit Lions 37, Chicago Bears 34
10/18/15 Pittsburgh Steelers 25, Arizona Cardinals 13
10/19/15 Philadelphia Eagles 27, New York Giants 7
10/22/15 Seattle Seahawks 20, San Francisco 49ers 3
10/25/15 New York Giants 27, Dallas Cowboys 20
10/25/15 Washington Redskins 31, Tampa Bay Buccaneers 30
10/25/15 Oakland Raiders 37, San Diego Chargers 29
10/25/15 Carolina Panthers 27, Philadelphia Eagles 16
10/25/15 New England Patriots 30, New York Jets 23
10/25/15 Minnesota Vikings 28, Detroit Lions 19
10/25/15 Miami Dolphins 44, Houston Texans 26
10/25/15 St. Louis Rams 24, Cleveland Browns 6
10/25/15 New Orleans Saints 27, Indianapolis Colts 21
10/25/15 Atlanta Falcons 10, Tennessee Titans 7
10/25/15 Kansas City Chiefs 23, Pittsburgh Steelers 13
10/25/15 Jacksonville Jaguars 34, Buffalo Bills 31

Using C++ , write a program using structs and the string class. Read a file containing team names, their conferences and divisions. This data will be stored in
Using C++ , write a program using structs and the string class. Read a file containing team names, their conferences and divisions. This data will be stored in
Using C++ , write a program using structs and the string class. Read a file containing team names, their conferences and divisions. This data will be stored in
Using C++ , write a program using structs and the string class. Read a file containing team names, their conferences and divisions. This data will be stored in
Using C++ , write a program using structs and the string class. Read a file containing team names, their conferences and divisions. This data will be stored in
Using C++ , write a program using structs and the string class. Read a file containing team names, their conferences and divisions. This data will be stored in
Using C++ , write a program using structs and the string class. Read a file containing team names, their conferences and divisions. This data will be stored in
Using C++ , write a program using structs and the string class. Read a file containing team names, their conferences and divisions. This data will be stored in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site