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
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:
Get Answers For Free
Most questions answered within 1 hours.