Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5. #include <iomanip>
  6. #include <cctype>
  7.  
  8. using namespace std;
  9.  
  10. struct Player {
  11.     string name;
  12.     int number;
  13.     int point;
  14.     void addPoints(int p) {
  15.         point += p;
  16.     }
  17. };
  18.  
  19. Player getInfo(int i)
  20. {
  21.     string mystr, n;
  22.     Player playerInfo;
  23.     int num;
  24.     double pt;
  25.     cout << "Please enter the information for player " << i + 1 << endl;
  26.     cout << "The player's name is ";
  27.     getline(cin, n);
  28.     playerInfo.name = n;
  29.  
  30.     do {
  31.         cout << "The player's number is ";
  32.         getline(cin, mystr);
  33.         stringstream(mystr) >> num;
  34.         if (num < 0)
  35.         {
  36.             cout << "Number cannot be negative\n";
  37.         }
  38.     } while (num<0);
  39.  
  40.     playerInfo.number = num;
  41.  
  42.     do {
  43.         cout << "The player's point is ";
  44.         getline(cin, mystr);
  45.         stringstream(mystr) >> pt;
  46.         if (pt < 0)
  47.         {
  48.             cout << "Point cannot be negative\n";
  49.         }
  50.     } while (pt<0);
  51.  
  52.     playerInfo.point = pt;
  53.  
  54.     return playerInfo;
  55. }
  56.  
  57.  
  58.  
  59. void print(const vector<Player> &playlist)
  60. {
  61.     double totalPoint = 0, biggestPoint = 0, biggestIndex = 0;
  62.     cout << setw(15) << "List" << setw(15) << "Name" << setw(15) << "Number" << setw(15) << "Points" << endl;
  63.     for (int i = 0; i<playlist.size(); i++)
  64.     {
  65.         cout << setw(12) << i + 1 << setw(18) << playlist.at(i).name << setw(15) << playlist.at(i).number << setw(15) << playlist.at(i).point << endl;
  66.         totalPoint += playlist.at(i).point;
  67.         if (playlist.at(i).point>biggestPoint)
  68.         {
  69.             biggestPoint = playlist.at(i).point;
  70.             biggestIndex = i;
  71.         }
  72.     }
  73.     cout << setw(45) << "Total Point" << setw(15) << totalPoint << endl;
  74.     cout << "The player " << playlist.at(biggestIndex).name << " has the highest point of " << playlist.at(biggestIndex).point << endl;
  75.  
  76. }
  77.  
  78. int main()
  79. {
  80.     const int NUM_PLAYER = 3;
  81.     vector<Player> playerlist;
  82.     string mystr;
  83.     char ch;
  84.     int numberAdd, pointAdd;
  85.  
  86.     for (int i = 0; i < NUM_PLAYER; i++)
  87.     {
  88.         playerlist.push_back(getInfo(i));
  89.     }
  90.  
  91.     do {
  92.         cout << "Do you want to add point for some player? (\"Y\" for YES, or anyother letter to quit) ";
  93.         getline(cin, mystr);
  94.         stringstream(mystr) >> ch;
  95.         if (toupper(ch) == 'Y')
  96.         {
  97.             cout << "Player's number: ";
  98.             getline(cin, mystr);
  99.             stringstream(mystr) >> numberAdd;
  100.             cout << "Adding point: ";
  101.             getline(cin, mystr);
  102.             stringstream(mystr) >> pointAdd;
  103.             for (int i = 0; i < NUM_PLAYER; i++)
  104.             {
  105.                 if (numberAdd == playerlist.at(i).number)
  106.                 {
  107.                     playerlist.at(i).addPoints(pointAdd);
  108.                 }
  109.             }
  110.         }
  111.     } while (toupper(ch) == 'Y');
  112.  
  113.     print(playerlist);
  114.  
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement