Advertisement
riggnaros

Football Array

Nov 28th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.67 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <fstream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. struct PlayerInfo {
  10.     string FName;
  11.     string LName;
  12.     string position;
  13.     int touchdowns;
  14.     int catches;
  15.     int passingYards;
  16.     int rushingYards;
  17.     int recYards;
  18. };
  19.  
  20. void showMenu(); //
  21. void InputData(PlayerInfo players[]); //
  22. void SaveData(PlayerInfo players[]); //
  23. void printPlayerData(PlayerInfo players[], int i); //
  24. void printData(PlayerInfo players[]); //
  25. int searchArray(PlayerInfo players[], string lName, string fname); //
  26. void updateRecYards(PlayerInfo p[], int r, int playerIndex); //
  27. void updateTouchDowns(PlayerInfo p[], int td, int playerIndex); //
  28. void updateCatches(PlayerInfo p[], int catches, int playerIndex); //
  29. void updatePassingYards(PlayerInfo p[], int passingYards, int playerIndex); //
  30. void updateRushingYards(PlayerInfo p[], int rushingYards, int playerIndex); //
  31.  
  32. int main()
  33. {
  34.     PlayerInfo players[11];
  35.     int playerIndex;
  36.     string LName, FName;
  37.     int choice;
  38.     int numOfTouchDowns, catches, numOfPassingYards, numOfRecYards, numOfRushingYards;
  39.     InputData(players);
  40.     do {
  41.         cout << endl;
  42.         showMenu();
  43.         cin >> choice;
  44.         cout << endl;
  45.  
  46.         switch (choice) {
  47.         case 1:
  48.             cout << endl
  49.                  << "Enter player's last name to search for: ";
  50.             cin >> LName;
  51.             cout << endl
  52.                  << "Enter player's first name to search for: ";
  53.             cin >> FName;
  54.             playerIndex = searchArray(players, LName, FName);
  55.             if (playerIndex == 99) {
  56.                 cout << "No such player found";
  57.             }
  58.             else
  59.                 printPlayerData(players, playerIndex);
  60.             break;
  61.         case 2:
  62.             printData(players);
  63.             break;
  64.         case 3:
  65.             cout << endl
  66.                  << "Enter player's last name to search for: ";
  67.             cin >> LName;
  68.             cout << endl
  69.                  << "Enter player's first name to search for: ";
  70.             cin >> FName;
  71.             playerIndex = searchArray(players, LName, FName);
  72.             cout << "Enter number of touchdowns to be added: ";
  73.             cin >> numOfTouchDowns;
  74.             if (playerIndex == 99) {
  75.                 cout << "No such player found";
  76.             }
  77.             else
  78.                 updateTouchDowns(players, numOfTouchDowns, playerIndex);
  79.             break;
  80.         case 4:
  81.             cout << endl
  82.                  << "Enter player's last name to search for: ";
  83.             cin >> LName;
  84.             cout << endl
  85.                  << "Enter player's first name to search for: ";
  86.             cin >> FName;
  87.             playerIndex = searchArray(players, LName, FName);
  88.             cout << "Enter number of catches to be added: ";
  89.             cin >> catches;
  90.             if (playerIndex == 99) {
  91.                 cout << "No such player found";
  92.             }
  93.             else
  94.                 updateCatches(players, catches, playerIndex);
  95.             break;
  96.         case 5:
  97.             cout << endl
  98.                  << "Enter player's last name to search for: ";
  99.             cin >> LName;
  100.             cout << endl
  101.                  << "Enter player's first name to search for: ";
  102.             cin >> FName;
  103.             playerIndex = searchArray(players, LName, FName);
  104.             cout << "Enter number of passing yards to be added: ";
  105.             cin >> numOfPassingYards;
  106.             if (playerIndex == 99) {
  107.                 cout << "No such player found";
  108.             }
  109.             else
  110.                 updatePassingYards(players, numOfPassingYards, playerIndex);
  111.             break;
  112.         case 6:
  113.             cout << endl
  114.                  << "Enter player's last name to search for: ";
  115.             cin >> LName;
  116.             cout << endl
  117.                  << "Enter player's first name to search for: ";
  118.             cin >> FName;
  119.             playerIndex = searchArray(players, LName, FName);
  120.             cout << "Enter number of rushing yards to be added: ";
  121.             cin >> numOfRushingYards;
  122.             if (playerIndex == 99) {
  123.                 cout << "No such player found";
  124.             }
  125.             else
  126.                 updateRushingYards(players, numOfRushingYards, playerIndex);
  127.             break;
  128.         case 7:
  129.             cout << endl
  130.                  << "Enter player's last name to search for: ";
  131.             cin >> LName;
  132.             cout << endl
  133.                  << "Enter player's first name to search for: ";
  134.             cin >> FName;
  135.             playerIndex = searchArray(players, LName, FName);
  136.             cout << "Enter number of receiving yards to be added: ";
  137.             cin >> numOfRecYards;
  138.             if (playerIndex == 99) {
  139.                 cout << "No such player found";
  140.             }
  141.             else
  142.                 updateRecYards(players, numOfRecYards, playerIndex);
  143.             break;
  144.  
  145.         case 99:
  146.             break;
  147.  
  148.         default:
  149.             cout << "Invalid selection." << endl;
  150.         }
  151.     } while (choice != 99);
  152.     char response;
  153.  
  154.     cout << "Would you like to save data: (y,Y/n,N) ";
  155.     cin >> response;
  156.     cout << endl;
  157.  
  158.     if (response == 'y' || response == 'Y') {
  159.         SaveData(players);
  160.     }
  161.  
  162.     return 0;
  163. }
  164.  
  165. void showMenu()
  166. {
  167.     cout << "\nSelect one of the following options: " << endl
  168.          << "1: To print a player's data" << endl
  169.          << "2: To print the entire data" << endl
  170.          << "3: To update a player's touchdowns" << endl
  171.          << "4: To update a player's number of catches" << endl
  172.          << "5: To update a player's passing yards" << endl
  173.          << "6: To update a player's rushing yards" << endl
  174.          << "7: To update a player's receiving yards" << endl
  175.          << "99: To quit the program" << endl
  176.          << endl;
  177. };
  178.  
  179. int searchArray(PlayerInfo players[], string LName, string FName)
  180. {
  181.     for (int i = 0; i < 11; i++) {
  182.  
  183.         if (LName == players[i].LName) {
  184.             if (FName == players[i].FName) {
  185.                 return i;
  186.             }
  187.         }
  188.     }
  189.     return 99;
  190. };
  191.  
  192. void updateTouchDowns(PlayerInfo p[], int td, int playerIndex)
  193. {
  194.     p[playerIndex].touchdowns = p[playerIndex].touchdowns + td;
  195. };
  196.  
  197. void updateRecYards(PlayerInfo p[], int recYards, int playerIndex)
  198. {
  199.     p[playerIndex].recYards = p[playerIndex].recYards + recYards;
  200. };
  201. void updateCatches(PlayerInfo p[], int catches, int playerIndex)
  202. {
  203.     p[playerIndex].catches = p[playerIndex].catches + catches;
  204. };
  205. void updatePassingYards(PlayerInfo p[], int passingYards, int playerIndex)
  206. {
  207.     p[playerIndex].passingYards = p[playerIndex].passingYards + passingYards;
  208. };
  209. void updateRushingYards(PlayerInfo p[], int rushingYards, int playerIndex)
  210. {
  211.     p[playerIndex].rushingYards = p[playerIndex].rushingYards + rushingYards;
  212. };
  213.  
  214. void printData(PlayerInfo players[])
  215. {
  216.     for (int i = 0; i < 11; i++) {
  217.  
  218.        cout << players[i].FName << ' '
  219.             << players[i].LName << ' '
  220.             << players[i].position << ' '
  221.             << players[i].touchdowns << ' '
  222.             << players[i].catches << ' '
  223.             << players[i].passingYards << ' '
  224.             << players[i].rushingYards << ' '
  225.             << players[i].recYards << ' '
  226.            << endl;
  227.     }
  228. };
  229.  
  230. void printPlayerData(PlayerInfo players[], int i)
  231.  
  232. {
  233.    cout << players[i].FName << ' '
  234.         << players[i].LName << ' '
  235.         << players[i].position << ' '
  236.         << players[i].touchdowns << ' '
  237.         << players[i].catches << ' '
  238.         << players[i].passingYards << ' '
  239.         << players[i].rushingYards << ' '
  240.         << players[i].recYards << ' '
  241.        << endl;
  242. };
  243.  
  244. void InputData(PlayerInfo players[])
  245. {
  246.     ifstream infile;
  247.     infile.open("footballdatafile.txt");
  248.  
  249.     for (int i = 0; i < 11; i++) {
  250.         infile >> players[i].FName
  251.             >> players[i].LName
  252.             >> players[i].position
  253.             >> players[i].touchdowns
  254.             >> players[i].catches
  255.             >> players[i].passingYards
  256.             >> players[i].rushingYards
  257.             >> players[i].recYards;
  258.     }
  259.     infile.close();
  260. };
  261.  
  262. void SaveData(PlayerInfo players[])
  263. {
  264.     ofstream outfile;
  265.  
  266.     outfile.open("footballdatafile.txt");
  267.  
  268.     for (int i = 0; i < 11; i++) {
  269.         outfile << players[i].FName << ' '
  270.                << players[i].LName << ' '
  271.                << players[i].position << ' '
  272.                << players[i].touchdowns << ' '
  273.                << players[i].catches << ' '
  274.                << players[i].passingYards << ' '
  275.                << players[i].rushingYards << ' '
  276.                << players[i].recYards  << endl;
  277.     }
  278.     outfile.close();
  279. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement