Advertisement
Garey

Kursova

Dec 12th, 2018
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.03 KB | None | 0 0
  1. /**
  2.  * \brief A program that uses files as database for operating a football players card-index.
  3.  *
  4.  * This program has the functionality of:
  5.  *      - Adding new players
  6.  *      - Searching players by ID
  7.  *      - Searching players by Country
  8.  *      - Outputting all players
  9.  *      - Modifying goals scored by player's last name and country
  10.  *
  11.  * Things that can be added:
  12.  *      - Removing from file
  13.  *      - Modifying other player data, such as name, country, etc.
  14.  *      - More searching methods, such as by name, last_name, goals scored, etc.
  15.  *      - A lot of tweaks could be done to the player information
  16.  *      - etc...
  17.  *
  18.  * \version: 1.0
  19.  *
  20.  * \date 2018/12/12 13:44:06
  21.  *
  22.  */
  23. #include <iostream>
  24. #include <string>
  25. #include <vector>
  26.  
  27. #include <fstream>
  28.  
  29. #include <conio.h>
  30.  
  31. using namespace std;
  32.  
  33. /*!
  34.  * A structure that contains the Football Player information
  35.  */
  36. struct Player {
  37.     string first_name;
  38.     string last_name;
  39.     string country;
  40.  
  41.     size_t id;
  42.     size_t scored_goals;
  43. };
  44.  
  45. /*!
  46.  * \brief This function asks the user to input data, makes some checks
  47.  *      and if the checks pass, saves the information to a file named
  48.  *      players.dat by converting the struct into binary code.
  49.  *
  50.  * \param Player An array of custom data structure representing the data, may be
  51.  *      empty, but not null.
  52.  * \param array_size A std::size_t defined variable used for the number of elements
  53.  *      in the array we mentioned earlier (Player array).
  54.  *
  55.  * \see std::size_t
  56.  * \see auto specifier
  57.  */
  58. auto input(Player* array, size_t array_size) -> void;
  59.  
  60. /*!
  61.  * \brief This function gets the information from the binary file and converts
  62.  *      it to its' representative data type and variable name of the structure.
  63.  *      It is the core function of the queries as well. As long as you understand
  64.  *      how this function works, the others will seem like they are the same.
  65.  *      (which they are, I just copied them from this function and added up more
  66.  *      features).
  67.  *
  68.  * \param Player An array of custom data structure representing the data, may be
  69.  *      empty, but not null.
  70.  *
  71.  * \see auto specifier
  72.  */
  73. auto output(Player* array) -> void;
  74.  
  75. /*!
  76.  * \brief This function gets the information from the binary file and converts
  77.  *      it to its' representative data type and variable name of the structure
  78.  *      by a specific argument.In this example, we are using the ID to search
  79.  *      for the player(s) we want to show. The function itself asks the user to
  80.  *      input a player ID that he wants to search for in the file.
  81.  *
  82.  *      For in-depth understanding, look at QueryByCountry function
  83.  *
  84.  * \param Player An array of custom data structure representing the data, may be
  85.  *      empty, but not null.
  86.  *
  87.  * \see auto specifier
  88.  * \see QueryByCountry
  89.  */
  90. auto QueryById(Player* array) -> void;
  91.  
  92. /*!
  93.  * \brief This function gets the information from the binary file and converts
  94.  *      it to its' representative data type and variable name of the structure
  95.  *      by a specific argument.In this example, we are using the country to search
  96.  *      for the player(s) we want to show. The function itself asks the user to
  97.  *      input a player country that he wants to search for in the file.
  98.  *
  99.  *      For in-depth understanding, look at QueryByCountry function
  100.  *
  101.  * \param Player An array of custom data structure representing the data, may be
  102.  *      empty, but not null.
  103.  *
  104.  * \see std::size_t
  105.  * \see auto specifier
  106.  */
  107. auto QueryByCountry(Player* array) -> void;
  108.  
  109. /*!
  110.  * \brief This function modifies the information from the binary file and converts
  111.  *      it to its' representative data type and variable name of the structure
  112.  *      by a specific argument.In this example, we are using the players' country
  113.  *      and the players' last name to search for the player(s) we want to modify.
  114.  *      The function itself asks the user to input the player's country and
  115.  *      last name that he wants to search for in the file. Then if any records are
  116.  *      found, asks the user to enter goals scored. The goals scored has validation
  117.  *      check. If this check is passed, modifies the current player's goals scored.
  118.  *      If multiple players found with this country and last name, modifies them
  119.  *      one by one by the algorithm described above.
  120.  *
  121.  *      For in-depth understanding, look at QueryByCountry function
  122.  *
  123.  * \param Player An array of custom data structure representing the data, may be
  124.  *      empty, but not null.
  125.  *
  126.  * \see std::size_t
  127.  * \see auto specifier
  128.  */
  129. auto ModifyData(Player* array) -> void;
  130.  
  131. /*!
  132.  * The main function which contains:
  133.  *  - A menu of choices for each function with the file. You can easily do:
  134.  *      -# Inputting data into file
  135.  *      -# Outputting the information from the file
  136.  *      -# Querying players by ID
  137.  *      -# Querying players by Country
  138.  *      -# Modifying goals scored on each player, queried by last name and country
  139.  *
  140.  * \brief The main function has been realized with pointers and dynamically allocation of
  141.  *      array. It has checks, that disallow negative data being used. It uses files to
  142.  *      store the information that has been passed and used in the program.
  143.  *
  144.  * \return int
  145.  *
  146.  * \see input
  147.  * \see output
  148.  * \see QueryById
  149.  * \see QueryByCountry
  150.  * \see ModifyData
  151.  */
  152. int main()
  153. {
  154.     size_t choice, array_size(30);
  155.  
  156.     /// Setting initial value different than null, because of fread function
  157.     Player* array = new Player[array_size];
  158.  
  159.     do {
  160.         system("cls");
  161.  
  162.         cout << "Menu: \n";
  163.         cout << "[1] Input players\n";
  164.         cout << "[2] Output Players\n";
  165.         cout << "[3] Query players by ID\n";
  166.         cout << "[4] Query players by Country\n";
  167.         cout << "[5] Modify player data, queried by name and last name\n\n";
  168.         cout << "[0] Exit\n";
  169.        
  170.         cout << "Make your choice: ";
  171.         cin >> choice;
  172.  
  173.         switch (choice) {
  174.             case 1:
  175.                 system("cls");
  176.  
  177.                 do {
  178.                     cout << "Enter number of players u want to input: ";
  179.                     cin >> array_size;
  180.  
  181.                 } while (array_size <= 0);
  182.  
  183.                 array = new Player[array_size];
  184.  
  185.                 input(array, array_size);
  186.  
  187.                 cout << "\n\nEnter any key to get back to the menu...\n";
  188.                 _getch();
  189.  
  190.                 break;
  191.             case 2:
  192.                 system("cls");
  193.                
  194.                 output(array);
  195.  
  196.                 cout << "\n\nEnter any key to get back to the menu...\n";
  197.                 _getch();
  198.  
  199.                 break;
  200.             case 3:
  201.                 system("cls");
  202.  
  203.                 QueryById(array);
  204.  
  205.                 cout << "\n\nEnter any key to get back to the menu...\n";
  206.                 _getch();
  207.  
  208.                 break;
  209.  
  210.             case 4:
  211.                 system("cls");
  212.                 cin.ignore();
  213.  
  214.                 QueryByCountry(array);
  215.  
  216.  
  217.                 cout << "\n\nEnter any key to get back to the menu...\n";
  218.                 _getch();
  219.  
  220.                 break;
  221.  
  222.             case 5:
  223.                 system("cls");
  224.  
  225.                 ModifyData(array);
  226.  
  227.                 cout << "\n\nEnter any key to get back to the menu...\n";
  228.                 _getch();
  229.  
  230.                 break;
  231.         }
  232.     } while (choice != 0);
  233.  
  234.     /// Clearing the memory we used
  235.     delete []array;
  236.  
  237.     return 0;
  238. }
  239.  
  240. auto input(Player* array, size_t array_size) -> void {
  241.    
  242.     fstream file("players.dat", ios::binary | ios::out);
  243.  
  244.     for (size_t i = 0; i < array_size; i++) {
  245.         cout << "Enter first name: ";
  246.         cin >> array[i].first_name;
  247.  
  248.         cout << "Enter last name: ";
  249.         cin >> array[i].last_name;
  250.  
  251.         cin.ignore();
  252.  
  253.         cout << "Enter country: ";
  254.         getline(cin, array[i].country);
  255.  
  256.  
  257.         do {
  258.             cout << "Enter player id: ";
  259.             cin >> array[i].id;
  260.         } while (array[i].id < 0);
  261.  
  262.         do {
  263.             cout << "Enter scored goals: ";
  264.             cin >> array[i].scored_goals;
  265.         } while (array[i].scored_goals < 0);
  266.     }
  267.  
  268.     file.write((char *)array, array_size * sizeof(Player));
  269.     file.close();
  270. }
  271.  
  272. auto output(Player* array) -> void {
  273.     /// Opening the file players.dat as binary for reading
  274.     fstream file("players.dat", ios::binary | ios::in);
  275.  
  276.     /// Check if the file is present, else exits
  277.     if (!file) {
  278.         cout << "File not found!\n";
  279.         exit(0);
  280.     }
  281.     else {
  282.         /// Goes to the end of the file
  283.         file.seekg(0L, ios::end);
  284.  
  285.         /// Gets the byte size of the whole file
  286.         auto pos = file.tellg();
  287.  
  288.         /*!
  289.          * Calculate the needed size of the array.
  290.          *
  291.          * How? We have the file byte size and our struct
  292.          *      represents just one item from our array.
  293.          *      To get our array size, we need to divide
  294.          *      the byte size of the file, where we stored
  295.          *      the array of Player, and the size in bytes
  296.          *      of our struct Player, thus: sizeof(Player).
  297.          *
  298.          * \see Player
  299.          */
  300.         auto array_size = pos / sizeof(Player);
  301.  
  302.         /// Getting back to the beginning of the file to read it
  303.         file.seekg(0L, ios::beg);
  304.  
  305.         /*!
  306.          * Reading the whole file, because we have array_size * sizeof(Player),
  307.          *      as a constant string(char *), and writing it by its' places in the struct.
  308.          *      - id from the file goes to id from the struct
  309.          *      - first_name from the file goes to first_name from the struct
  310.          *      - etc...
  311.          *
  312.          * \see Player
  313.          */
  314.         file.read((char *) array, array_size * sizeof(Player));
  315.  
  316.         /// Iterating through the array and outputting the information
  317.         for (size_t i = 0; i < array_size; i++) {
  318.             cout << "ID: " << array[i].id << endl;
  319.             cout << "First Name: " << array[i].first_name << endl;
  320.             cout << "Last Name: " << array[i].last_name << endl;
  321.             cout << "Country: " << array[i].country << endl;
  322.             cout << "Goals scored: " << array[i].scored_goals << endl;
  323.  
  324.             cout << "\n\n----------------------------------------------\n\n";
  325.         }
  326.     }
  327.  
  328.     /// Closing the file
  329.     file.close();
  330. }
  331.  
  332. auto QueryById(Player* array) -> void {
  333.  
  334.     size_t id, counter(0);
  335.  
  336.     cout << "Enter ID you want to search for: ";
  337.     cin >> id;
  338.  
  339.     fstream file("players.dat", ios::binary | ios::in);
  340.  
  341.     if (!file) {
  342.         cout << "File not found!\n";
  343.         exit(0);
  344.     }
  345.     else {
  346.         file.seekg(0L, ios::end);
  347.  
  348.         auto pos = file.tellg();
  349.         auto array_size = pos / sizeof(Player);
  350.  
  351.         file.seekg(0L, ios::beg);
  352.  
  353.         file.read((char *)array, array_size * sizeof(Player));
  354.  
  355.         for (size_t i = 0; i < array_size; i++) {
  356.             if (array[i].id == id) {
  357.                 cout << "ID: " << array[i].id << endl;
  358.                 cout << "First Name: " << array[i].first_name << endl;
  359.                 cout << "Last Name: " << array[i].last_name << endl;
  360.                 cout << "Country: " << array[i].country << endl;
  361.                 cout << "Goals scored: " << array[i].scored_goals << endl;
  362.  
  363.                 counter++;
  364.             }
  365.  
  366.             cout << "\n\n----------------------------------------------\n\n";
  367.         }
  368.     }
  369.  
  370.     if (counter == 0) {
  371.         cout << "No players with this id found!\n\n";
  372.     }
  373.  
  374.     file.close();
  375. }
  376.  
  377. auto QueryByCountry(Player* array) -> void {
  378.  
  379.     /// Used for user input
  380.     char choice;
  381.     string country;
  382.  
  383.     /// Counter used for information only
  384.     size_t counter(0);
  385.  
  386.     /// Opening the file players.dat as binary for reading
  387.     fstream file("players.dat", ios::binary | ios::in);
  388.    
  389.     /// Checks if the file has opened correctly
  390.     if (!file) {
  391.         cout << "File not found!\n";
  392.         exit(0);
  393.     }
  394.     else {
  395.         /// Setting the pointer to the end of the file
  396.         file.seekg(0L, ios::end);
  397.  
  398.         /// Get the whole file byte size.
  399.         auto pos = file.tellg();
  400.  
  401.         /*!
  402.          * Calculate the needed size of the array.
  403.          *
  404.          * How? We have the file byte size and our struct
  405.          *      represents just one item from our array.
  406.          *      To get our array size, we need to divide
  407.          *      the byte size of the file, where we stored
  408.          *      the array of Player, and the size in bytes
  409.          *      of our struct Player, thus: sizeof(Player).
  410.          *
  411.          * \see Player
  412.          */
  413.         auto array_size = pos / sizeof(Player);
  414.  
  415.         /// Getting back to the beginning of the file to read it
  416.         file.seekg(0L, ios::beg);
  417.  
  418.         /*!
  419.          * Reading the whole file, because we have array_size * sizeof(Player),
  420.          *      as a constant string(char *), and writing it by its' places in the struct.
  421.          *      - id from the file goes to id from the struct
  422.          *      - first_name from the file goes to first_name from the struct
  423.          *      - etc...
  424.          *
  425.          * \see Player
  426.          */
  427.         file.read((char *)array, array_size * sizeof(Player));
  428.  
  429.         do {
  430.            
  431.             /*!
  432.              * Inputting what country we want to search for.
  433.              *
  434.              * \see getline
  435.              * \see std::string
  436.              */
  437.             cout << "Enter country you want to search for: ";
  438.             getline(cin, country);
  439.  
  440.             /// Iterating through the array
  441.             for (size_t i = 0; i < array_size; i++) {
  442.                 /// Check if each element's country is equal to what we inputted
  443.                 if (array[i].country == country) {
  444.                     cout << "ID: " << array[i].id << endl;
  445.                     cout << "First Name: " << array[i].first_name << endl;
  446.                     cout << "Last Name: " << array[i].last_name << endl;
  447.                     cout << "Country: " << array[i].country << endl;
  448.                     cout << "Goals scored: " << array[i].scored_goals << endl;
  449.  
  450.                     /// Counter used for information only
  451.                     counter++;
  452.                 }
  453.  
  454.                 cout << "\n\n----------------------------------------------\n\n";
  455.             }
  456.  
  457.             /// Counter used for information only
  458.             if (counter == 0) {
  459.                 cout << "No players with this country found!\n\n";
  460.             }
  461.  
  462.             /*!
  463.              * Asking the user if he wants to query another country.
  464.              *
  465.              * \see _getch()
  466.              * \see char
  467.              */
  468.             cout << "Do you want to search for another country? (y/n) ";
  469.             choice = _getch();
  470.            
  471.             /*!
  472.              * While the user enters anything different than the character n
  473.              *      do this whole thing with the asking and printing.
  474.              */
  475.         } while (choice != 'n');
  476.     }
  477.  
  478.     /// Closing the opened file
  479.     file.close();
  480. }
  481.  
  482. auto ModifyData(Player* array) -> void {
  483.  
  484.     string last_name, country;
  485.  
  486.     size_t counter(0);
  487.  
  488.     cout << "Enter Country you want to search for: ";
  489.     cin >> country;
  490.  
  491.     cout << "Enter Last Name you want to search for: ";
  492.     cin >> last_name;
  493.  
  494.     ifstream file("players.dat", ios::binary | ios::in);
  495.  
  496.     if (!file) {
  497.         cout << "File not found!\n";
  498.         exit(0);
  499.     }
  500.     else {
  501.         file.seekg(0L, ios::end);
  502.  
  503.         auto pos = file.tellg();
  504.         auto array_size = pos / sizeof(Player);
  505.  
  506.         file.seekg(0L, ios::beg);
  507.  
  508.         file.read((char *)array, array_size * sizeof(Player));
  509.  
  510.         for (size_t i = 0; i < array_size; i++) {
  511.             if (array[i].country == country && array[i].last_name == last_name) {
  512.                 size_t goals_scored;
  513.  
  514.                 cout << "ID: " << array[i].id << endl;
  515.                 cout << "First Name: " << array[i].first_name << endl;
  516.                 cout << "Last Name: " << array[i].last_name << endl;
  517.                 cout << "Country: " << array[i].country << endl;
  518.                 cout << "Goals scored: " << array[i].scored_goals << endl;
  519.  
  520.                 cout << "\n\n";
  521.  
  522.                 cout << "Modifying goals scored...\n\n";
  523.  
  524.                 do {
  525.                     cout << "Enter goals scored: ";
  526.                     cin >> goals_scored;
  527.                 } while (goals_scored < 0);
  528.  
  529.                 /// Modifying the value
  530.                 array[i].scored_goals = goals_scored;
  531.  
  532.                 cout << "\n\nModified values:\n\n";
  533.  
  534.                 cout << "ID: " << array[i].id << endl;
  535.                 cout << "First Name: " << array[i].first_name << endl;
  536.                 cout << "Last Name: " << array[i].last_name << endl;
  537.                 cout << "Country: " << array[i].country << endl;
  538.                 cout << "Goals scored: " << array[i].scored_goals << endl;
  539.                 counter++;
  540.             }
  541.  
  542.             cout << "\n\n----------------------------------------------\n\n";
  543.         }
  544.  
  545.         if (counter == 0) {
  546.             cout << "No players with this combination of country and last name found!\n\n";
  547.             file.close();
  548.         }
  549.         else {
  550.             file.close();
  551.  
  552.             cout << "\n\nModifying file...\n\n";
  553.            
  554.             /// Modifying the file with the new modified structure
  555.             fstream file("players.dat", ios::binary | ios::out);
  556.  
  557.             file.write((char *)array, array_size * sizeof(Player));
  558.  
  559.             file.close();
  560.         }
  561.     }
  562. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement