I'm trying to implement a player class that contains the following attributes:
-> Name
-> Position Played
-> High School Graduated From
-> Height
-> Scoring Average
-> Rebounding Average
-> GPA
-> Seasons Of Eligibility Remaining
And the class must have the following functions:
-> Sort list of objects by name.
-> Sort list of objects by scoring average.
-> Get list of objects that contain a GPA of over 3.0.
-> Sort list of objects by high school graduated from.
This is what I've come up with so far:
player.h
Code:
// Player Header File (player.h)
#include "apstring.h"
class player
{
public:
// Constructors
player();
player(apstring name, apstring position, apstring high_school, int height, double average, double rebounding, double gpa, int seasons);
player(const player &p);
// Destructor
~player();
// Accessors
apstring get_name() const;
apstring get_position() const;
apstring get_high_school() const;
int get_height() const;
double get_scoring_average() const;
double get_rebounding_average() const;
double get_gpa() const;
int get_seasons() const;
// Assignment
const player& operator = (const player &p);
// Modifiers
void set_name(apstring name);
void set_position(apstring position);
void set_high_school(apstring high_school);
void set_height(int height);
void set_scoring_average(double scoring_average);
void set_rebounding_average(double rebounding_average);
void set_gpa(double gpa);
void set_seasons(int seasons);
// Other Methods
void sort_names(player p[ ], int length);
void sort_scoring_averages(player p[ ], int length);
void sort_gpas(player p[ ], int length);
void sort_high_schools(player p[ ], int length);
void print(player p[ ], int length);
private:
// Private Data
apstring my_name, my_position, my_high_school;
double my_scoring_average, my_rebounding_average, my_gpa;
int my_height, my_seasons;
// Private Methods
void swap(player &x, player &y);
};
player.cpp
Code:
// Player Class Implementation (player.cpp)
#include "player.h"
// Constructors
player::player()
{
my_name = "";
my_position = "";
my_high_school = "";
my_height = 0;
my_scoring_average = 0.00;
my_rebounding_average = 0.00;
my_gpa = 0.00;
my_seasons = 0;
}
player::player(apstring name, apstring position, apstring high_school, int height, double scoring_average, double rebounding_average, double gpa, int seasons)
{
my_name = name;
my_position = position;
my_high_school = high_school;
my_height = height;
my_scoring_average = scoring_average;
my_rebounding_average = rebounding_average;
my_gpa = gpa;
my_seasons = seasons;
}
player::player(const player &p)
{
my_name = p.my_name;
my_position = p.my_position;
my_high_school = p.my_high_school;
my_height = p.my_height;
my_scoring_average = p.my_scoring_average;
my_rebounding_average = p.my_rebounding_average;
my_gpa = p.my_gpa;
my_seasons = p.my_seasons;
}
// Destructor
player::~player()
{
}
// Accessors
apstring player::get_name() const
{
return my_name;
}
apstring player::get_position() const
{
return my_position;
}
apstring player::get_high_school() const
{
return my_high_school;
}
int player::get_height() const
{
return my_height;
}
double player::get_scoring_average() const
{
return my_scoring_average;
}
double player::get_rebounding_average() const
{
return my_rebounding_average;
}
double player::get_gpa() const
{
return my_gpa;
}
int player::get_seasons() const
{
return my_seasons;
}
// Assignment
const player& player::operator = (const player &p)
{
if (this != &p)
{
my_name = p.my_name;
my_position = p.my_position;
my_high_school = p.my_high_school;
my_height = p.my_height;
my_scoring_average = p.my_scoring_average;
my_rebounding_average = p.my_rebounding_average;
my_gpa = p.my_gpa;
my_seasons = p.my_seasons;
}
return *this;
}
// Modifiers
void player::set_name(apstring name)
{
my_name = name;
}
void player::set_position(apstring position)
{
my_position = position;
}
void player::set_high_school(apstring high_school)
{
my_high_school = high_school;
}
void player::set_height(int height)
{
my_height = height;
}
void player::set_scoring_average(double scoring_average)
{
my_scoring_average = scoring_average;
}
void player::set_rebounding_average(double rebounding_average)
{
my_rebounding_average = rebounding_average;
}
void player::set_gpa(double gpa)
{
my_gpa = gpa;
}
void player::set_seasons(int seasons)
{
my_seasons = seasons;
}
// Other Methods
void player::sort_names(player a[ ], int length)
{
int min_index = 0;
for (int j = 0; j < length - 1; ++j)
{
for (int k = j + 1; k < length; ++k)
if (a[k].get_name() < a[min_index].get_name())
min_index = k;
}
swap(a[j], a[min_index]);
}
void player::sort_scoring_averages(player a[ ], int length)
{
int min_index = 0;
for (int j = 0; j < length - 1; ++j)
{
for (int k = j + 1; k < length; ++k)
if (a[k].get_scoring_average() < a[min_index].get_scoring_average())
min_index = k;
}
swap(a[j], a[min_index]);
}
void player::sort_gpas(player a[ ], int length)
{
int min_index = 0;
for (int j = 0; j < length - 1; ++j)
{
for (int k = j + 1; k < length; ++k)
if (a[k].get_gpa() < a[min_index].get_gpa())
min_index = k;
}
swap(a[j], a[min_index]);
}
void player::sort_high_schools(player a[ ], int length)
{
int min_index = 0;
for (int j = 0; j < length - 1; ++j)
{
for (int k = j + 1; k < length; ++k)
if (a[k].get_high_school() < a[min_index].get_high_school())
min_index = k;
}
swap(a[j], a[min_index]);
}
void player::print(player a[ ], int length)
{
cout << "Name / Position Played / High School Graduated From / Height / Scoring Average / Rebounding Average / GPA / Seasons Of Eligibility Remaining" << endl;
for (int j = 0; j < length; ++j)
cout << a[j].get_name() << " " << a[j].get_position() << " " << a[j].get_high_school() << " " << a[j].get_height() << " " << a[j].get_scoring_average() << " " << a[j].get_rebounding_average() << " " << a[j].get_gpa() << " " << a[j].get_seasons() << endl;
}
// Private Methods
void player::swap(player &x, player &y)
{
player temp = x;
x = y;
y = temp;
}
basketball_players.cpp
Code:
#include <iostream.h>
#include <fstream.h>
#include "player.h"
void main()
{
// Declarations
apstring file, line, name, position, high_school;
double scoring_average, rebounding_average, gpa;
ifstream in_file;
int counter = 0, num_players = 0, height, seasons;
player *b_player;
// Get file name.
cout << "Enter file name." << endl;
cin >> file;
// Get number of players.
cout << "Enter number of players." << endl;
cin >> num_players;
// Create dynamic array for players.
b_player = new player[num_players];
// Open file.
in_file.open(file.c_str());
// Prime in_file.
in_file >> name;
// Assemble each set of data into an object (inside an array of objects).
while (! in_file.eof())
{
in_file >> position >> high_school >> height >> scoring_average >> rebounding_average >> gpa >> seasons;
b_player[counter].set_name(name);
b_player[counter].set_position(position);
b_player[counter].set_high_school(high_school);
b_player[counter].set_height(height);
b_player[counter].set_scoring_average(scoring_average);
b_player[counter].set_rebounding_average(rebounding_average);
b_player[counter].set_gpa(gpa);
b_player[counter].set_seasons(seasons);
in_file >> name;
++counter;
}
// Close file.
in_file.close();
// Sort by name and print.
cout << "Sorted By Name" << endl;
b_player->sort_names(b_player, num_players);
b_player->print(b_player, num_players);
cout << endl;
// Sort by scoring average and print.
cout << "Sorted By Scoring Averages" << endl;
b_player->sort_scoring_averages(b_player, num_players);
b_player->print(b_player, num_players);
cout << endl;
// Print all players who have a GPA of 3.0 or higher.
cout << "All Players With GPA Of 3.0 Or Higher" << endl;
b_player->sort_gpas(b_player, num_players);
b_player->print(b_player, num_players);
cout << endl;
// Sort by high school and print.
cout << "Sorted By High School" << endl;
b_player->sort_high_schools(b_player, num_players);
b_player->print(b_player, num_players);
}
What the basketball_players.cpp file does is read a list of players and their information from a file and convert each line of information into a player object. For some reason the sort methods (like sort_names, sort_scoring_averages, sort_gpas, and sort_high_schools methods) are not working the way they should. When the lists are printed out, the lists are the same each time.
I know this post was long, but I hope somebody can figure out why those methods are not being done correctly.
I greatly appreciate any help.