Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <vector>
- #include <iomanip>
- #include <cctype>
- using namespace std;
- struct Player {
- string name;
- int number;
- int point;
- void addPoints(int p) {
- point += p;
- }
- };
- Player getInfo(int i)
- {
- string mystr, n;
- Player playerInfo;
- int num;
- double pt;
- cout << "Please enter the information for player " << i + 1 << endl;
- cout << "The player's name is ";
- getline(cin, n);
- playerInfo.name = n;
- do {
- cout << "The player's number is ";
- getline(cin, mystr);
- stringstream(mystr) >> num;
- if (num < 0)
- {
- cout << "Number cannot be negative\n";
- }
- } while (num<0);
- playerInfo.number = num;
- do {
- cout << "The player's point is ";
- getline(cin, mystr);
- stringstream(mystr) >> pt;
- if (pt < 0)
- {
- cout << "Point cannot be negative\n";
- }
- } while (pt<0);
- playerInfo.point = pt;
- return playerInfo;
- }
- void print(const vector<Player> &playlist)
- {
- double totalPoint = 0, biggestPoint = 0, biggestIndex = 0;
- cout << setw(15) << "List" << setw(15) << "Name" << setw(15) << "Number" << setw(15) << "Points" << endl;
- for (int i = 0; i<playlist.size(); i++)
- {
- cout << setw(12) << i + 1 << setw(18) << playlist.at(i).name << setw(15) << playlist.at(i).number << setw(15) << playlist.at(i).point << endl;
- totalPoint += playlist.at(i).point;
- if (playlist.at(i).point>biggestPoint)
- {
- biggestPoint = playlist.at(i).point;
- biggestIndex = i;
- }
- }
- cout << setw(45) << "Total Point" << setw(15) << totalPoint << endl;
- cout << "The player " << playlist.at(biggestIndex).name << " has the highest point of " << playlist.at(biggestIndex).point << endl;
- }
- int main()
- {
- const int NUM_PLAYER = 3;
- vector<Player> playerlist;
- string mystr;
- char ch;
- int numberAdd, pointAdd;
- for (int i = 0; i < NUM_PLAYER; i++)
- {
- playerlist.push_back(getInfo(i));
- }
- do {
- cout << "Do you want to add point for some player? (\"Y\" for YES, or anyother letter to quit) ";
- getline(cin, mystr);
- stringstream(mystr) >> ch;
- if (toupper(ch) == 'Y')
- {
- cout << "Player's number: ";
- getline(cin, mystr);
- stringstream(mystr) >> numberAdd;
- cout << "Adding point: ";
- getline(cin, mystr);
- stringstream(mystr) >> pointAdd;
- for (int i = 0; i < NUM_PLAYER; i++)
- {
- if (numberAdd == playerlist.at(i).number)
- {
- playerlist.at(i).addPoints(pointAdd);
- }
- }
- }
- } while (toupper(ch) == 'Y');
- print(playerlist);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement