Question

C++ Programming   You are to develop a program to read Baseball player statistics from an input...

C++ Programming

  You are to develop a program to read Baseball player statistics from an input file. Each player should bestored in a Player object. Therefore, you need to define the Player class. Each player will have a firstname, last name, a position (strings) and a batting average (floating point number).
Your class needs to provide all the methods needed to read, write, initialize the object.
Your data needs to be stored in an array of player objects. The maximum size of the array should be 50for this version of the program. Make sure that you 1) keep track of how many players have beenstored in the array and, 2) you do not overwrite the end of the array if there happens to be more than50 players in the data file.
An input data file will be used to provide the data.  Each player is written in the file, one per line, in thefollowing form
              FIRSTNAME LASTNAME POSITION BATTINGAVERAGE
Note that the data is separated by blanks only. There are no commas or other separator characters inthe input data.  You will need to create your own file for testing.

Make sure to use detailed comments

Summary of Operation
 Prompt the user for the input and output file names. DO NOT hardcode file names into yourprogram.

 Open input file

 Read each player and store them in an array of Player objects

 Keep track of the number of players in the array

 Open an output file

 Write each player from the array into the output file, along with any other output required bythe assignment.

 Remember to close your files when done with them

Make sure to use detailed comments

Example Input:

Chipper Jones 3B 0.303

Rafael Furcal SS 0.281

Hank Aaron RF 0.305

Example Output:

There were 3 players in the input data file:

Jones, Chipper: 3B (0.303)

Furcal, Rafael: SS (0.281)

Aaron, Hank: RF (0.305

Homework Answers

Answer #1

C++ Program:

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

using namespace std;

//Player class
class Player
{
    //Private variables
    private:
        string firstName, lastName, position;
        float battingAvg;

    public:

        //Default constructor
        Player()
        {
            firstName = "";
            lastName = "";
            position = "";
            battingAvg = 0;
        }

        //Parameterized constructor
        Player(string fname, string lname, string pos, float bavg)
        {
            firstName = fname;
            lastName = lname;
            position = pos;
            battingAvg = bavg;
        }

        //Function that returns player info
        string getPlayer()
        {
            stringstream ss;

            //Returning player details in the specified format
            ss << lastName << ", " << firstName << ": " << position << " (" << battingAvg << ")";

            return ss.str();
        }
};


//Main function
int main()
{
    string tfname, tlname, tpos;
    float tbatAvg;

    //Array to hold Player objects
    Player players[50];

    int playerCount = 0, i;

    string ipFile, opFile;

    //Reading input file name
    cout << "\n Input file name: ";
    cin >> ipFile;

    //Reading output file name
    cout << "\n Output file name: ";
    cin >> opFile;

    //Opening input file
    fstream fin(ipFile, ios::in);

    //Checking input file existence
    if(!fin.good())
    {
        cout << "\n Error!! Cannot open input file.... Exiting.... \n";
        return -1;
    }

    //Reading data from file
    while(fin.good() && playerCount < 50)
    {
        //Fetching data
        fin >> tfname >> tlname >> tpos >> tbatAvg;

        //Creating object
        Player obj(tfname, tlname, tpos, tbatAvg);

        //Storing in array
        players[playerCount] = obj;

        //Incrementing player count
        playerCount++;
    }

    //Opening output file
    fstream fout(opFile, ios::out);

    //Writing data to output file
    for(i=0; i<playerCount; i++)
    {
        //Fetching each player data
        fout << players[i].getPlayer() << endl;
    }

    cout << endl << " Output file generated.... " << endl;

    //Closing files
    fin.close();
    fout.close();

    cout << endl << endl;
    return 0;
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:

Know the answer?
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
Similar Questions
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input...
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input file has an unknown number of numeric values(could be empty too).Read the numbers from the input fileand store even numbers in one arrayand odd numbers in another array.Create menu options to Display count of even numbers, count of odd numbersand the sum values in each array Determine the average of each array Determine the median of each array Sort each array in ascending order(use...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Homework 3 Before attempting this project, be sure you have completed all of the reading assignments,...
Homework 3 Before attempting this project, be sure you have completed all of the reading assignments, hands-on labs, discussions, and assignments to date. Create a Java class named HeadPhone to represent a headphone set. The class contains:  Three constants named LOW, MEDIUM and HIGH with values of 1, 2 and 3 to denote the headphone volume.  A private int data field named volume that specifies the volume of the headphone. The default volume is MEDIUM.  A private...
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies...
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the accounts. Its main() method uses the CustomerAccounts class to manage a collection of CustomerAccount objects: reading customer accounts data & transactions, and obtaining a String showing the customer accounts data after the operations are complete. You will need to complete the readCustomerAccounts () and applyTransactions() methods in the CustomerAccounts class. First, please fill in your name in the...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...