Baseball Player Program

Picture of the output messages and possible inputsThis program keeps track of the information of up to nine baseball players. When the program begins, it asks the user for the number of baseball players that will be entered and validate that number appropriately. It asks for the information about each player, by number, and then by name.

Image of the sorted outputAfter all the players have been added, the program prints the players two ways. First, sorted alphabetically by name. Then, prints the players sorted by slugging percentage from highest to lowest. Both use appropriate column widths and show the percentages with three decimal places.

Code

BaseballPlayerMain.cpp:
// Main file for BaseballPlayer Class
// Daniel Ruiz

#include "BaseballPlayer.h"
#include 
#include 
#include 

using namespace std;

const int MAXPLAYERS = 9;

void readPlayerInfo(BaseballPlayer baseballPlayer[], int i)
{
	string first, last;
	int atBat, sin, doub, trip, homeR;

	cout << "What is the first name of player " << i << "?" << endl;
	cin >> first;
	cout << "What is the last name of player " << i << "?" << endl;
	cin >> last;

	baseballPlayer[i].setName(first, last);

	cout << "How many at-bats did " << baseballPlayer[i].getName() << " have?" << endl;
	cin >> atBat;
	cout << "How many singles did " << baseballPlayer[i].getName() << " have?" << endl;
	cin >> sin;
	cout << "How many doubles did " << baseballPlayer[i].getName() << " have?" << endl;
	cin >> doub;
	cout << "How many triples did " << baseballPlayer[i].getName() << " have?" << endl;
	cin >> trip;
	cout << "How many home runs did " << baseballPlayer[i].getName() << " have?" << endl;
	cin >> homeR;

	baseballPlayer[i].setStats(atBat, sin, doub, trip, homeR);
}

void selectionSortName(BaseballPlayer baseballPlayer[], int numPlayers) {
	int i = 0;
	int j = 0;
	int indexSmallest = 0;
	BaseballPlayer temp;  // Temporary variable for swap

	for (i = 1; i < numPlayers; ++i) {

		// Find index of smallest remaining element
		indexSmallest = i;
		for (j = i + 1; j < numPlayers; ++j) {

			if (baseballPlayer[j].getName() < baseballPlayer[indexSmallest].getName()) {
				indexSmallest = j;
			}
		}

		// Swap numbers[i] and numbers[indexSmallest]
		temp = baseballPlayer[i];
		baseballPlayer[i] = baseballPlayer[indexSmallest];
		baseballPlayer[indexSmallest] = temp;
	}
}

void selectionSortSlug(BaseballPlayer baseballPlayer[], int numPlayers) {
	int i = 0;
	int j = 0;
	int indexLargest = 0;
	BaseballPlayer temp;  // Temporary variable for swap

	for (i = 1; i < numPlayers; ++i) {

		// Find index of smallest remaining element
		indexLargest = i;
		for (j = i + 1; j < numPlayers; ++j) {

			if (baseballPlayer[j].getSlg() > baseballPlayer[indexLargest].getSlg()) {
				indexLargest = j;
			}
		}

		// Swap numbers[i] and numbers[indexSmallest]
		temp = baseballPlayer[i];
		baseballPlayer[i] = baseballPlayer[indexLargest];
		baseballPlayer[indexLargest] = temp;
	}
}

void printPlayers(BaseballPlayer baseballPlayer[], int i)
{
	cout << right << setw(20) << baseballPlayer[i].getName();
	cout << right << setw(6) << fixed << setprecision(3) << baseballPlayer[i].getAvg();
	cout << right << setw(6) << fixed << setprecision(3) << baseballPlayer[i].getSlg() << endl;

}

int main() {
	
	int numPlayers = MAXPLAYERS;
	BaseballPlayer baseballPlayer[MAXPLAYERS];
	int i;

	cout << "How many baseball players are there?" << endl;
	cin >> numPlayers;
	numPlayers = numPlayers + 1;

	// for loop to take in all players info
	for (int i = 1; i < numPlayers; i++) {
		readPlayerInfo(baseballPlayer, i);
	}
	
	cout << "Players by name:" << endl;
	cout << right << setw(20) << "Name";
	cout << right << setw(6) << "SLG";
	cout << right << setw(6) << "AVG" << endl;
	
	selectionSortName(baseballPlayer, numPlayers); //sort players by getName
	
	for (int i = 1; i < numPlayers; i++) {
	printPlayers(baseballPlayer, i);
	}
	cout << endl;

	cout << "Players by slugging:" << endl;
	cout << right << setw(20) << "Name";
	cout << right << setw(6) << "SLG";
	cout << right << setw(6) << "AVG" << endl;

	selectionSortSlug(baseballPlayer, numPlayers); //sort players by getSlg

	for (int i = 1; i < numPlayers; i++) {
	printPlayers(baseballPlayer, i);
	}

	return 0;
}
BaseballPlayer.h:
// Header file for BaseballPlayer Class
// Daniel Ruiz

#ifndef BASEBALLPLAYER_H  // (include guard) if baseballplayer is not defined
#define BASEBALLPLAYER_H
#include 

using namespace std;

class BaseballPlayer
{
private:
	string fname;
	string lname;
	int AB;
	int singles;
	int doubles;
	int triples;
	int HR;
public:
	BaseballPlayer();
	string getName() const;
	void setName(string, string);
	int getAB() const;
	int getSingles() const;
	int getDoubles() const;
	int getTriples() const;
	int getHR() const;
	double getAvg() const;
	double getSlg() const;
	void setStats(int, int, int, int, int);
	void init(string, string);
};
#endif
BaseballPlayer.cpp:
// CPP file for BaseballPlayer Class
// Daniel Ruiz

#include "BaseballPlayer.h"
#include 

string BaseballPlayer::getName() const
{
	return (fname + " " + lname);
}

void BaseballPlayer::setName(string first, string last)
{
	fname = first;
	lname = last;
}

int BaseballPlayer::getAB() const
{
	return AB;
}

int BaseballPlayer::getSingles() const
{
	if ((singles + doubles + triples + HR) > AB) {
		return 1;
	}
	else {
		return singles;
	}
}

int BaseballPlayer::getDoubles() const
{
	if ((singles + doubles + triples + HR) > AB) {
		return 1;
	}
	else {
	return doubles;
	}
}

int BaseballPlayer::getTriples() const
{
	if ((singles + doubles + triples + HR) > AB) {
		return 1;
	}
	else {
		return triples;
	}
}

int BaseballPlayer::getHR() const
{
	return HR;
}

double BaseballPlayer::getAvg() const
{
	double AVG;
	if (AB == 0) {
		return 0;
	}
	if (singles == 0 && doubles == 0 && triples == 0 && HR == 0) {
		AVG = 0;
		return AVG;
	}
	if ((singles + doubles + triples + HR) > AB) {
		AVG = 4.0 / double(AB);
		return AVG;
	}
	else {
		AVG = (double(singles) + double(doubles) + double(triples) + double(HR)) / double(AB);
		return AVG;
	}
}

double BaseballPlayer::getSlg() const
{
	double SLG;
	if (AB == 0) {
		return 0;
	}
	if (singles == 0 && doubles == 0 && triples == 0 && HR == 0) {
		SLG = 0;
		return SLG;
	}
	if ((singles + doubles + triples + HR) > AB) {
		SLG = (singles + doubles + triples + HR) / AB;
		return SLG;
	}
	if ((singles + doubles + triples + HR) < AB) {
		SLG = (double(singles) + (double(doubles) * 2) + (double(triples) * 3) + (double(HR) * 4)) / AB;
		return SLG;
	}
	else {
		SLG = (singles + (doubles * 2) + (triples * 3) + (HR) * 4) / AB;
		return SLG;
	}
}

void BaseballPlayer::setStats(int atBat, int sin, int doub, int trip, int homeR)
{
	if (atBat >= 0) {
		AB = atBat;
	}
	if (sin >= 0) {
		singles = sin;
	}
	if (doub >= 0) {
		doubles = doub;
	}
	if (trip >= 0) {
		triples = trip;
	}
	if (homeR >= 0) {
		HR = homeR;
	}
}

BaseballPlayer::BaseballPlayer()
{
	fname = "None";
	lname = "None";
	AB = 0;
	singles = 0;
	doubles = 0;
	triples = 0;
	HR = 0;
}

void BaseballPlayer::init(string first, string last)
{
	fname = first; 
	lname = last;
}