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
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined. Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and...
Project File Processing. Write a program that will read in from input file one line at...
Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces,...
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...
You are to write a C program that will read from a file, one or more...
You are to write a C program that will read from a file, one or more sets of x,y coordinates. Each set of coordinates is part of a Cartesian system. A Cartesian coordinate system is a system that specifies each point uniquely in a plane by a pair of numerical coordinates. Your program will determine which quadrant each set belong. - Quadrants are often numbered 1st - 4th and denoted by Roman numerals: I(+,+), II (−,+), III (−,−), and IV...
Write a program that accepts as input the mass, in grams, and density, in grams per...
Write a program that accepts as input the mass, in grams, and density, in grams per cubic centimeters, and outputs the volume of the object using the formula: volume = mass / density. Format your output to two decimal places. ** Add Comments ** Print Name and Assignment on screen ** Date ** Submit .cpp file. Demo // This program uses a type cast to avoid an integer division. #include <iostream> // input - output stream #include <fstream> //working file...
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...
Hi there, I've been asked to write a program in C which can read values from...
Hi there, I've been asked to write a program in C which can read values from a file then sort them, and then write to a binary file. I'm getting stuck when I write my binary file as the output is just spitting out garbage values and not the values that are being read in. When I print my input file reader everything is perfect but after sorting and then writing, the output is completely wrong. I have checked that...
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...
Write a Java program that Reads baseball data in from a comma delimited file. Each line...
Write a Java program that Reads baseball data in from a comma delimited file. Each line of the file contains a name followed by a list of symbols indicating the result of each at bat: 1 for single, 2 for double, 3 for triple, 4 for home run, o for out, w for walk, s for sacrifice Statistics are computed and printed for each player. EXTRA CREDIT (+10 points); compute each player's slugging percentage https://www.wikihow.com/Calculate-Slugging-Percentage Be sure to avoid a...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT