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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/*
Kattis Program: nba.cpp
Date: 3/19/2019
Version: 3
Author: PiggiesGoSqueal
Status: Work in progress
Program Information:
- A C++ Command Line Interface (CLI)-based menu-driven program that
helps a client keep track of NBA players’ stats with certain
requirements.
Algorithm Steps:
1. Create a STRUCT that allows storing information for players. (DONE)
Must be able to store info for:
- player’s name, team name, points, rebounds, and assists (DONE)
- Be sure it is set up in such a way that allows easily storing (DONE)
a TON of players.
2. Using a function, the program should read the existing players'
stats from the players.txt file. (DONE)
3. Program should store data from players.txt into the struct. (DONE)
4. Program should be menu driven, giving users various choices. (Work in progress)
- Program must use a function to display players’ stats in a
cleanly formatted tabular form. | text | text |
---------------
| | |
---------------
5. Use function(s) to search the vector to find the index of a
specific player, and update the data of an existing player.
6. Use a function to add a new player’s stat.
7. Use a function to sort the players’ stats based on player’s name and
display the sorted array of records.
* Tried doing with sort() algorithm. Doesn't seem to work? So may need to switch back to bubblesort.
*/
#include <iostream>
#include <string>
#include <vector>
#include <fstream> // for file reading and writing
#include <iomanip> // for formatting
#include <algorithm> // for sort()
using namespace std;
struct playerData {
string pName; // player name
string tName; // team name
double points;
double rebounds;
double assists;
};
// Function Declarations:
// Reading player data from players.txt & storing it:
// Using references (&) to directly update info without returning anything.
// Remember: pointers (*) point directly to the ADDRESS of a variable, not the variable itself.
void readPData(ifstream& playersFile, vector<playerData>& playerList);
// Sorts player stats based on player name (alphabetical order)
// Will also display the sorted vector of records
void sortData(vector<playerData>& playerList);
// Prints a menu.
// Allows user to select some options.
void printMenu(const vector<playerData>& playerList);
int main() {
// Declaring player list vector which contains all player "objects"
// for the struct.
vector<playerData> pList;
// Opening players.txt file:
ifstream pFile; // input file
string pFileName = "players.txt";
pFile.open(pFileName);
if (!pFile)
cerr << "File could not be opened.\n";
// reads pFile data and stores information using pList[i].<struct_stuff>
readPData(pFile, pList);
printMenu(pList);
// sorts player data based on player name (alphabetical order)
// Will also display the sorted array of records
//sortData(pList);
pFile.close();
cout << "[Debug] Done!\n";
return 0;
}
/*
The following function will:
- Read the contents of the file and assign it's contents into
playerList[i].pname, playerList[i].tname, etc.
- Because vector<playerData>& playerList is using a reference, it should auto
update everything for that without needing to return anything for
the function.
*/
void readPData(ifstream& playersFile, vector<playerData>& playerList) {
playerData temp;
// While players file has not reached the end of file:
for (int i = 0; getline(playersFile, temp.pName); i++) {
// playerList[i] is a player in the list. And playerList[i] has it's own parts (such as pName, tName, etc).
// This for loop will go through all players in the file and store their info into the array.
// I getline() for pName in the for loop, thus I don't add it here.
getline(playersFile, temp.tName); // this is for team name
playersFile >> temp.points;
playersFile >> temp.rebounds;
playersFile >> temp.assists;
playersFile.ignore(1000, '\n');
playerList.push_back(temp); // adds all parts above to playerList vector (of type playerData) all at once.
}
}
// This function should sort the player's stats based on the player's name (alphabetical order). - Doesn't work at the moment
// Then should display the sorted array of records. (This part isn't started yet)
void sortData(vector<playerData>& playerList) {
for (int i = 0; i < playerList.size(); i++) {
// should sort all player names in vector. If it does, I'll NEED TO ADD TNAME, POINTS, ETC.
sort(playerList[i].pName.begin(), playerList[i].pName.end());
// prints sorted vector contents:
cout << playerList[i].pName << " "
<< playerList[i].tName << " "
<< playerList[i].points << " "
<< playerList[i].rebounds << " "
<< playerList[i].assists << " \n";
}
cout << "[Debug] sortData() function done!\n";
}
// Used in printMenu(), allows easily printing stuff in menu.
template<typename T> void printElem(T t, const int& width) {
cout << left << setw(width) << setfill(' ') << t;
}
/*
4. Program should be menu driven, giving users various choices.
- Program must use a function to display players’ stats in a
cleanly formatted tabular form. | text | text |
---------------
text text
text text
* Tabs: player name | team name | points | rebounds | assists |
*/
void printMenu(const vector<playerData>& playerList) {
const int spaces = 4;
const int nameWidth = 6;
const int numWidth = 8;
const int lengthM = 69; // length of menu (used for dashes)
// Prints Tabs:
// | player name | team name | points | rebounds | assists |
cout << setfill(' ')
<< setw(spaces+10) << "player name" << setw(spaces)
<< setw(spaces+8) << "team name" << setw(spaces)
<< setw(spaces+5) << "points" << setw(spaces)
<< setw(spaces+7) << "rebounds" << setw(spaces)
<< setw(spaces+6) << "assists" << setw(spaces)
<< "\n"
<< setfill('-') << setw(lengthM) << '\n';
// Prints actual data:
for (int i = 0; i < playerList.size(); i++) {
//cout << "[Debug] " << playerList[i].pName << endl;
printElem(playerList[i].pName, nameWidth);
printElem(playerList[i].tName, nameWidth);
printElem(playerList[i].points, numWidth);
printElem(playerList[i].rebounds, numWidth);
printElem(playerList[i].assists, numWidth);
cout << endl;
}
cout << "[Debug] printMenu() done! \n";
}
|