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
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
struct Player {
std::string name;
std::string role;
unsigned score {};
};
std::istream& operator>>(std::istream& is, Player& play) {
getline(is, play.name, ',');
getline(is, play.role, ',');
is >> play.score;
return is.ignore();
}
std::ostream& operator<<(std::ostream& os, const Player& play) {
return os << play.name << " " << play.role << " " << play.score << '\n';
}
void addrec(std::vector<Player>& players) {
Player play;
std::cout << "Enter player name: ";
std::getline(std::cin, play.name);
std::cout << "Enter role: ";
std::getline(std::cin, play.role);
std::cout << "Enter score: ";
std::cin >> play.score;
std::cin.ignore();
players.push_back(play);
}
void displayplayers(const std::vector<Player>& players) {
for (const auto& play : players)
if (!play.name.empty())
std::cout << play;
}
void deleteplayer(std::vector<Player>& players) {
std::string nam;
size_t cnt {};
std::cout << "Enter player's name: ";
std::getline(std::cin, nam);
for (auto& p : players)
if (p.name == nam) {
p.name.clear();
++cnt;
}
if (!cnt)
std::cout << "Player not found\n";
else
std::cout << cnt << " records deleted\n";
}
void search(const std::vector<Player>& players) {
std::string nam;
size_t cnt {};
std::cout << "Enter player's name: ";
std::getline(std::cin, nam);
for (const auto& p : players)
if (p.name == nam) {
std::cout << p;
++cnt;
}
if (!cnt)
std::cout << "Player not found\n";
else
std::cout << cnt << " records found\n";
}
int main() {
std::vector<Player> players;
std::ifstream ifs("players.txt");
if (!ifs)
std::cout << "Cannot open file\n";
else
for (Player play; ifs >> play; players.push_back(play));
for (int pick {}; pick != 6; ) {
std::cout << "\nPlayer List System\n";
std::cout << "1. Add Player\n";
std::cout << "2. Search Player\n";
std::cout << "3. Display Player List\n";
std::cout << "4. Delete Player\n";
std::cout << "5. Update Player\n";
std::cout << "6. Exit\n";
std::cout << "Pick an option: ";
std::cin >> pick;
std::cin.ignore();
switch (pick) {
case 1:
addrec(players);
break;
case 2:
search(players);
break;
case 3:
displayplayers(players);
break;
case 4:
deleteplayer(players);
break;
case 5:
//help in update record when you search a player's record, you may change its name, role and score
//when the searched player is not found, it will display no results found
break;
case 6:
break;
default:
std::cout << "Invalid option\n";
break;
}
}
std::ofstream ofs("players.txt");
if (!ofs)
std::cout << "Cannot open file\n";
else
for (const auto& [nam, rol, pnt] : players)
if (!nam.empty())
ofs << nam << ',' << rol << ',' << pnt << '\n';
}
|