1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int MAX_PLAYERS = 10; // Max number allowed in roster
string names[MAX_PLAYERS]
{
"----", "----", "----", "----", "----",
"----", "----", "----", "----", "----"
};
int numbers[MAX_PLAYERS] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
int ratings[MAX_PLAYERS] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
int avail_index = 0; // Next open index in roster, used in adding/removing
//Print roster
void PrintRoster()
{
cout << endl << endl;
cout << left <<
setw(7) << "Index" <<
setw(10) << "Player" <<
setw(10) << "Jersey#" <<
setw(10) << "Rating" << endl;
for(int i = 0; i < MAX_PLAYERS; i++)
{
cout << left <<
setw(7) << i <<
setw(10) << names[i] <<
setw(10) << numbers[i] <<
setw(10) << ratings[i] << endl;
}
cout << endl;
}
// Add a player to the next open spot
void AddPlayer(string name, int number, int rating)
{
if (avail_index == MAX_PLAYERS)
{
cout << "Sorry, unable to add " << name <<
" -- roster is full!" << endl;
return;
}
cout << "Adding player " << name << " to index "<< avail_index << endl;
names[avail_index] = name;
numbers[avail_index] = number;
ratings[avail_index] = rating;
avail_index += 1;
}
// Attempt to delete player from specified index
void DeletePlayer(int i)
{
if (i<0 || i>=avail_index)
{
cout << "Sorry, unable to delete player from index " << i << endl;
return;
}
cout << "Deleting player " << names[i] << " from index "<< i << endl;
// Shift those right of i to the left
for (int j=i; j<avail_index-1; ++j)
{
names[j] = names[j+1];
numbers[j] = numbers[j+1];
ratings[j] = ratings[j+1];
}
// Decrement and reset next available spot
avail_index -= 1;
names[avail_index] = "----";
numbers[avail_index] = -1;
ratings[avail_index] = -1;
}
int main()
{
PrintRoster();
AddPlayer("Gilberto", 39, 85);
AddPlayer("Abbigail", 81, 75);
AddPlayer("Gregory", 73, 89);
AddPlayer("Giovanna", 21, 90);
AddPlayer("Kole", 57, 70);
AddPlayer("Dennis", 5, 92);
AddPlayer("Shannon", 87, 66);
AddPlayer("Mekhi", 21, 61);
AddPlayer("Damari", 76, 97);
AddPlayer("Chelsea", 13, 88);
AddPlayer("Joanna", 44, 97);
AddPlayer("Kadyn", 58, 82);
PrintRoster();
DeletePlayer(10);
DeletePlayer(7);
PrintRoster();
DeletePlayer(8);
PrintRoster();
AddPlayer("Joanna", 44, 97);
AddPlayer("Kadyn", 58, 82);
PrintRoster();
return 0;
}
|